diff --git a/frontend/client/src/views/fields/link-parent.js b/frontend/client/src/views/fields/link-parent.js index e86a9f1192..e0186ecf75 100644 --- a/frontend/client/src/views/fields/link-parent.js +++ b/frontend/client/src/views/fields/link-parent.js @@ -17,28 +17,28 @@ * * 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.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { - return Dep.extend({ - + return Dep.extend({ + type: 'linkParent', - - listTemplate: 'fields.link.list', - - detailTemplate: 'fields.link.detail', - + + listTemplate: 'fields.link.list', + + detailTemplate: 'fields.link.detail', + editTemplate: 'fields.link-parent.edit', - + searchTemplate: 'fields.link-parent.search', - + nameName: null, - + idName: null, - + foreignScopeList: null, - + data: function () { return _.extend({ idName: this.idName, @@ -47,38 +47,38 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { idValue: this.model.get(this.idName), nameValue: this.model.get(this.nameName), typeValue: this.model.get(this.typeName), - foreignScope: this.foreignScope, + foreignScope: this.foreignScope, foreignScopeList: this.foreignScopeList, }, Dep.prototype.data.call(this)); }, - + setup: function () { this.nameName = this.name + 'Name'; this.typeName = this.name + 'Type'; this.idName = this.name + 'Id'; - + this.foreignScopeList = this.model.defs.links[this.name].entityList || []; - + if (this.mode == 'edit' && this.foreignScopeList.length == 0) { throw new Error('Bad parent link defenition. Model list is empty.'); } - - this.foreignScope = this.model.get(this.typeName) || this.foreignScopeList[0]; - + + this.foreignScope = this.model.get(this.typeName) || this.foreignScopeList[0]; + this.listenTo(this.model, 'change:' + this.typeName, function () { this.foreignScope = this.model.get(this.typeName) || this.foreignScopeList[0]; }.bind(this)); - - + + var self = this; - + if (this.mode != 'list') { this.addActionHandler('selectLink', function () { this.notify('Loading...'); this.createView('dialog', 'Modals.SelectRecords', { scope: this.foreignScope, createButton: this.mode != 'search' - }, function (dialog) { + }, function (dialog) { dialog.render(); Espo.Ui.notify(false); dialog.once('select', function (model) { @@ -87,35 +87,35 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { self.trigger('change'); }); }); - }); + }); this.addActionHandler('clearLink', function () { this.$elementName.val(''); this.$elementId.val(''); - this.trigger('change'); + this.trigger('change'); }); - + this.events['change select[name="' + this.typeName + '"]'] = function (e) { this.foreignScope = e.currentTarget.value; this.$elementName.val(''); this.$elementId.val(''); } - } + } }, - - afterRender: function () { - if (this.mode == 'edit' || this.mode == 'search') { + + afterRender: function () { + if (this.mode == 'edit' || this.mode == 'search') { this.$elementId = this.$el.find('input[name="' + this.idName + '"]'); this.$elementName = this.$el.find('input[name="' + this.nameName + '"]'); this.$elementType = this.$el.find('select[name="' + this.typeName + '"]'); - + this.$elementName.on('change', function () { - if (this.$elementName.val() == '') { + if (this.$elementName.val() == '') { this.$elementName.val(''); this.$elementId.val(''); this.trigger('change'); } }.bind(this)); - + if (this.mode == 'edit') { this.$elementName.on('blur', function (e) { if (this.model.has(this.nameName)) { @@ -127,7 +127,7 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { e.currentTarget.value = ''; }.bind(this)); } - + this.$elementName.autocomplete({ serviceUrl: function (q) { return this.foreignScope + '?orderBy=name&limit=7'; @@ -138,8 +138,8 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { return suggestion.name; }, transformResult: function (response) { - var response = JSON.parse(response); - var list = []; + var response = JSON.parse(response); + var list = []; response.list.forEach(function(item) { list.push({ id: item.id, @@ -150,7 +150,7 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { }, this); return { suggestions: list - }; + }; }.bind(this), onSelect: function (s) { this.$elementId.val(s.id); @@ -158,43 +158,53 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { this.trigger('change'); }.bind(this) }); - + var $elementName = this.$elementName; this.once('render', function () { $elementName.autocomplete('dispose'); - }, this); - + }, this); + this.once('remove', function () { $elementName.autocomplete('dispose'); - }, this); + }, this); } }, - + getValueForDisplay: function () { return this.model.get(this.model.get(this.nameName)); - }, - - fetch: function () { + }, + + validateRequired: function () { + if (this.params.required || this.model.isRequired(this.name)) { + if (this.model.get(this.idName) == null || !this.model.get(this.typeName)) { + var msg = this.translate('fieldIsRequired', 'messages').replace('{field}', this.translate(this.name, 'fields', this.model.name)); + this.showValidationMessage(msg); + return true; + } + } + }, + + fetch: function () { var data = {}; data[this.typeName] = this.$elementType.val() || null; data[this.nameName] = this.$elementName.val() || null; data[this.idName] = this.$elementId.val() || null; return data; }, - - fetchSearch: function () { - var id = this.$elementId.val(); + + fetchSearch: function () { + var id = this.$elementId.val(); var type = this.$elementType.val(); var name = this.$elementName.val() - + if (!name || !type) { return false; } - + var data = { type: 'and', - field: this.idName, - + field: this.idName, + value: [ { type: 'equals', @@ -206,13 +216,13 @@ Espo.define('Views.Fields.LinkParent', 'Views.Fields.Base', function (Dep) { field: this.typeName, value: type, } - ], + ], valueId: id, valueName: name, valueType: type, }; - return data; - }, + return data; + }, }); });