dynamic forms

This commit is contained in:
Yuri Kuznetsov
2014-07-10 13:08:10 +03:00
parent 2bfea69fe3
commit dc89aff7b1
3 changed files with 222 additions and 1 deletions
+16
View File
@@ -121,6 +121,22 @@ Espo.define('Controllers.Admin', 'Controller', function (Dep) {
model.fetch();
},
authentication: function () {
var model = this.getSettingsModel();
model.once('sync', function () {
model.id = '1';
this.main('Edit', {
model: model,
views: {
header: {template: 'admin.settings.header-authentication'},
body: {view: 'Admin.Authentication'},
},
});
}, this);
model.fetch();
},
rebuild: function (options) {
var master = this.get('master');
Espo.Ui.notify(master.translate('Please wait...'));
@@ -0,0 +1,122 @@
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
Espo.define('Views.Admin.Authentication', 'Views.Settings.Record.Edit', function (Dep) {
return Dep.extend({
layoutName: 'authentication',
dependencyDefs: {
'ldapAuth': {
map: {
true: [
{
action: 'show',
fields: ['ldapUsername', 'ldapPassword']
}
]
},
default: [
{
action: 'hide',
fields: ['ldapUsername', 'ldapPassword']
}
]
},
'ldapAccountCanonicalForm': {
map: {
'Backslash': [
{
action: 'show',
fields: ['ldapAccountDomainName', 'ldapAccountDomainNameShort']
}
],
'Principal': [
{
action: 'show',
fields: ['ldapAccountDomainName', 'ldapAccountDomainNameShort']
}
]
},
default: [
{
action: 'hide',
fields: ['ldapAccountDomainName', 'ldapAccountDomainNameShort']
}
]
}
},
setup: function () {
Dep.prototype.setup.call(this);
this.methodList = this.getMetadata().get('entityDefs.Settings.fields.authenticationMethod.options') || [];
this.authFields = {
'LDAP': [
'ldapHost', 'ldapPort', 'ldapAuth', 'ldapSecurity',
'ldapUsername', 'ldapPassword', 'ldapBindRequiresDn',
'ldapUserLoginFilter', 'ldapBaseDn', 'ldapAccountCanonicalForm',
'ldapAccountDomainName', 'ldapAccountDomainNameShort', 'ldapAccountDomainName',
'ldapAccountDomainNameShort', 'ldapTryUsernameSplit', 'ldapOptReferrals',
'ldapCreateEspoUser'
]
};
},
afterRender: function () {
this.handlePanelsVisibility();
this.listenTo(this.model, 'change:authenticationMethod', function () {
this.handlePanelsVisibility();
}, this);
},
handlePanelsVisibility: function () {
var authenticationMethod = this.model.get('authenticationMethod');
this.methodList.forEach(function (method) {
var list = (this.authFields[method] || []);
if (method != authenticationMethod) {
this.$el.find('.panel[data-panel-name="'+method+'"]').addClass('hidden');
list.forEach(function (field) {
this.hideField(field);
}, this);
} else {
this.$el.find('.panel[data-panel-name="'+method+'"]').removeClass('hidden');
list.forEach(function (field) {
this.showField(field);
}, this);
Object.keys(this.dependencyDefs || {}).forEach(function (attr) {
if (~list.indexOf(attr)) {
this._handleDependencyAttribute(attr);
}
}, this);
}
}, this);
},
});
});
+84 -1
View File
@@ -86,6 +86,8 @@ Espo.define('Views.Record.Detail', 'View', function (Dep) {
isWide: false,
dependencyDefs: {},
events: {
'click .button-container button': function (e) {
var $target = $(e.currentTarget);
@@ -350,7 +352,24 @@ Espo.define('Views.Record.Detail', 'View', function (Dep) {
this.listenTo(this.model, 'sync', function () {
this.attributes = this.model.getClonedAttributes();
}.bind(this));
},
this._initDependancy();
},
_initDependancy: function () {
this.dependencyDefs = _.extend(this.getMetadata().get('clientDefs.' + this.model.name + '.formDependency') || {}, this.dependencyDefs);
Object.keys(this.dependencyDefs || {}).forEach(function (attr) {
this.listenTo(this.model, 'change:' + attr, function () {
this._handleDependencyAttribute(attr);
}, this);
}, this);
this.on('after:render', function () {
this._handleDependencyAttributes();
}, this);
},
validate: function () {
var notValid = false;
@@ -642,6 +661,70 @@ Espo.define('Views.Record.Detail', 'View', function (Dep) {
}
this.getRouter().navigate(url, {trigger: true});
},
_handleDependencyAttributes: function () {
Object.keys(this.dependencyDefs || {}).forEach(function (attr) {
this._handleDependencyAttribute(attr);
}, this);
},
_handleDependencyAttribute: function (attr) {
var data = this.dependencyDefs[attr];
var value = this.model.get(attr);
if (value in (data.map || {})) {
(data.map[value] || []).forEach(function (item) {
this._doDependencyAction(item);
}, this);
} else {
if ('default' in data) {
(data.default || []).forEach(function (item) {
this._doDependencyAction(item);
}, this);
}
}
},
_doDependencyAction: function (data) {
var action = data.action;
var methodName = 'dependencyAction' + Espo.Utils.upperCaseFirst(action);
if (methodName in this && typeof this.methodName == 'function') {
this.methodName(data);
return;
}
var fields = data.fields || [];
switch (action) {
case 'hide':
fields.forEach(function (field) {
this.hideField(field);
}, this);
break;
case 'show':
fields.forEach(function (field) {
this.showField(field);
}, this);
break;
case 'setRequired':
fields.forEach(function (field) {
var fieldView = this.getFieldView(field);
if (fieldView) {
fieldView.setRequired();
}
}, this);
break;
case 'setNotRequired':
fields.forEach(function (field) {
var fieldView = this.getFieldView(field);
if (fieldView) {
fieldView.setRequired();
}
}, this);
break;
}
},
});
});