Skip to content

Commit

Permalink
Commit lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
zebulonj committed Apr 17, 2017
1 parent ea96635 commit 3c562fd
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FORMS_TOGGLE_SELECT = exports.FORMS_UPDATE_VALUE = exports.FORMS_INITIALIZE = exports.FORMS_CANCEL_OUTSIDE_CLICK = exports.FORMS_OUTSIDE_CLICK = undefined;
exports.initializeForm = initializeForm;
exports.updateFormValue = updateFormValue;
exports.toggleFormSelect = toggleFormSelect;
exports.cancelOutsideClick = cancelOutsideClick;

var _immutable = require('immutable');

var FORMS_OUTSIDE_CLICK = exports.FORMS_OUTSIDE_CLICK = 'FORMS_OUTSIDE_CLICK';
var FORMS_CANCEL_OUTSIDE_CLICK = exports.FORMS_CANCEL_OUTSIDE_CLICK = 'FORMS_CANCEL_OUTSIDE_CLICK';

var FORMS_INITIALIZE = exports.FORMS_INITIALIZE = 'FORMS_INITIALIZE';
var FORMS_UPDATE_VALUE = exports.FORMS_UPDATE_VALUE = 'FORMS_UPDATE_VALUE';
var FORMS_TOGGLE_SELECT = exports.FORMS_TOGGLE_SELECT = 'FORMS_TOGGLE_SELECT';

function initializeForm(form) {
var initialValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _immutable.Map)();

return {
type: FORMS_INITIALIZE,
form: form,
initialValues: initialValues
};
}

function updateFormValue(form, field, value) {
return {
type: FORMS_UPDATE_VALUE,
form: form,
field: field,
value: value
};
}

function toggleFormSelect(form, field) {
return {
type: FORMS_TOGGLE_SELECT,
form: form,
field: field
};
}

function cancelOutsideClick() {
return {
type: FORMS_CANCEL_OUTSIDE_CLICK
};
}
17 changes: 17 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _middleware = require('./middleware');

Object.keys(_middleware).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _middleware[key];
}
});
});
45 changes: 45 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.middleware = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _actions = require('./actions');

var defaultOptions = {
outsideAction: _actions.FORMS_OUTSIDE_CLICK
};

var middleware = exports.middleware = function middleware(options) {
return function (store) {
options = _extends({}, defaultOptions, options);

var _options = options,
outsideAction = _options.outsideAction;

var cancel = void 0;

if (document) {
document.body.addEventListener('click', function () {
cancel = false;

setTimeout(function () {
return !cancel && store.dispatch({ type: outsideAction });
}, 0);
}, true);
}

return function (next) {
return function (action) {
if ([_actions.FORMS_CANCEL_OUTSIDE_CLICK, _actions.FORMS_TOGGLE_SELECT].indexOf(action.type) !== -1) {
cancel = true;
}

return next(action);
};
};
};
};
36 changes: 36 additions & 0 deletions lib/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.$forms = $forms;

var _immutable = require('immutable');

var _actions = require('./actions');

var NEW_FORM = (0, _immutable.Map)({});

function $forms() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.Map)();
var action = arguments[1];

switch (action.type) {
case _actions.FORMS_OUTSIDE_CLICK:
return state.map(function (form) {
return form.set('$toggles', (0, _immutable.Map)());
});
case _actions.FORMS_INITIALIZE:
return state.set(action.form, NEW_FORM.merge({ $values: action.initialValues }));
case _actions.FORMS_UPDATE_VALUE:
return state.update(action.form, (0, _immutable.Map)(), function (form) {
return form.setIn(['$values', action.field], action.value);
});
case _actions.FORMS_TOGGLE_SELECT:
return state.updateIn([action.form, '$toggles', action.field], function (value) {
return !value;
});
default:
return state;
}
}

0 comments on commit 3c562fd

Please sign in to comment.