03f8fc6ccb563d71aa1e4db03a738d73c2f61257
2 var CRM
= (window
.CRM
) ? (window
.CRM
) : (window
.CRM
= {});
3 if (!CRM
.Designer
) CRM
.Designer
= {};
5 // TODO Optimize this class
6 CRM
.Designer
.PaletteFieldModel
= CRM
.Backbone
.Model
.extend({
9 * @var {string} required; a form-specific binding to an entity instance (eg 'student', 'mother')
18 initialize: function() {
20 getFieldSchema: function() {
21 return this.getRel('ufGroupModel').getFieldSchema(this.get('entityName'), this.get('fieldName'));
23 getLabel: function() {
24 // Note: if fieldSchema were a bit tighter, then we need to get a label from PaletteFieldModel at all
25 return this.getFieldSchema().title
|| this.get('fieldName');
27 getSectionName: function() {
28 // Note: if fieldSchema were a bit tighter, then we need to get a section from PaletteFieldModel at all
29 return this.getFieldSchema().section
|| 'default';
31 getSection: function() {
32 return this.getRel('ufGroupModel').getModelClass(this.get('entityName')).prototype.sections
[this.getSectionName()];
35 * Add a new UFField model to a UFFieldCollection (if doing so is legal).
36 * If it fails, display an alert.
38 * @param {int} ufGroupId
39 * @param {CRM.UF.UFFieldCollection} ufFieldCollection
40 * @param {Object} addOptions
41 * @return {CRM.UF.UFFieldModel} or null (if the field is not addable)
43 addToUFCollection: function(ufFieldCollection
, addOptions
) {
44 var name
, paletteFieldModel
= this;
45 var ufFieldModel
= paletteFieldModel
.createUFFieldModel(ufFieldCollection
.getRel('ufGroupModel'));
46 ufFieldModel
.set('uf_group_id', ufFieldCollection
.uf_group_id
);
47 if (!ufFieldCollection
.isAddable(ufFieldModel
)) {
49 ts('The field "%1" is already included.', {
50 1: paletteFieldModel
.getLabel()
57 ufFieldCollection
.add(ufFieldModel
, addOptions
);
58 // Load metadata and set defaults
59 // TODO: currently only works for custom fields
60 name
= this.get('fieldName').split('_');
61 if (name
[0] === 'custom') {
62 CRM
.api('custom_field', 'getsingle', {id
: name
[1]}, {success: function(field
) {
63 ufFieldModel
.set(_
.pick(field
, 'help_pre', 'help_post', 'is_required'));
68 createUFFieldModel: function(ufGroupModel
) {
69 var model
= new CRM
.UF
.UFFieldModel({
71 label
: this.getLabel(),
72 entity_name
: this.get('entityName'),
73 field_type
: this.getFieldSchema().civiFieldType
,
74 field_name
: this.get('fieldName')
83 * - ufGroupModel: UFGroupModel
85 CRM
.Designer
.PaletteFieldCollection
= CRM
.Backbone
.Collection
.extend({
86 model
: CRM
.Designer
.PaletteFieldModel
,
87 initialize: function(models
, options
) {
88 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
92 * Look up a palette-field
96 * @return {CRM.Designer.PaletteFieldModel}
98 getFieldByName: function(entityName
, fieldName
) {
99 return this.find(function(paletteFieldModel
) {
100 return (paletteFieldModel
.get('entityName') == entityName
&& paletteFieldModel
.get('fieldName') == fieldName
);
105 * Get a list of all fields, grouped into sections by "entityName+sectionName".
107 * @return {Object} keys are sections ("entityName+sectionName"); values are CRM.Designer.PaletteFieldModel
109 getFieldsByEntitySection: function() {
111 var fieldsByEntitySection
= this.groupBy(function(paletteFieldModel
) {
112 return paletteFieldModel
.get('entityName') + '-' + paletteFieldModel
.getSectionName();
114 return fieldsByEntitySection
;