Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | (function($) { |
2 | var CRM = (window.CRM) ? (window.CRM) : (window.CRM = {}); | |
3 | if (!CRM.Designer) CRM.Designer = {}; | |
4 | ||
5 | // TODO Optimize this class | |
6 | CRM.Designer.PaletteFieldModel = CRM.Backbone.Model.extend({ | |
7 | defaults: { | |
8 | /** | |
9 | * @var {string} required; a form-specific binding to an entity instance (eg 'student', 'mother') | |
10 | */ | |
11 | entityName: null, | |
12 | ||
13 | /** | |
14 | * @var {string} | |
15 | */ | |
16 | fieldName: null | |
17 | }, | |
18 | initialize: function() { | |
19 | }, | |
20 | getFieldSchema: function() { | |
21 | return this.getRel('ufGroupModel').getFieldSchema(this.get('entityName'), this.get('fieldName')); | |
22 | }, | |
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'); | |
26 | }, | |
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'; | |
30 | }, | |
31 | getSection: function() { | |
32 | return this.getRel('ufGroupModel').getModelClass(this.get('entityName')).prototype.sections[this.getSectionName()]; | |
33 | }, | |
34 | /** | |
35 | * Add a new UFField model to a UFFieldCollection (if doing so is legal). | |
36 | * If it fails, display an alert. | |
37 | * | |
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) | |
42 | */ | |
43 | addToUFCollection: function(ufFieldCollection, addOptions) { | |
8fd0e719 | 44 | var name, paletteFieldModel = this; |
6a488035 TO |
45 | var ufFieldModel = paletteFieldModel.createUFFieldModel(ufFieldCollection.getRel('ufGroupModel')); |
46 | ufFieldModel.set('uf_group_id', ufFieldCollection.uf_group_id); | |
47 | if (!ufFieldCollection.isAddable(ufFieldModel)) { | |
48 | CRM.alert( | |
49 | ts('The field "%1" is already included.', { | |
50 | 1: paletteFieldModel.getLabel() | |
51 | }), | |
52 | ts('Duplicate'), | |
53 | 'alert' | |
54 | ); | |
55 | return null; | |
56 | } | |
57 | ufFieldCollection.add(ufFieldModel, addOptions); | |
8fd0e719 CW |
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) { | |
ed619003 | 63 | ufFieldModel.set(_.pick(field, 'help_pre', 'help_post', 'is_required')); |
8fd0e719 CW |
64 | }}); |
65 | } | |
6a488035 TO |
66 | return ufFieldModel; |
67 | }, | |
68 | createUFFieldModel: function(ufGroupModel) { | |
69 | var model = new CRM.UF.UFFieldModel({ | |
70 | is_active: 1, | |
71 | label: this.getLabel(), | |
72 | entity_name: this.get('entityName'), | |
73 | field_type: this.getFieldSchema().civiFieldType, | |
74 | field_name: this.get('fieldName') | |
75 | }); | |
76 | return model; | |
77 | } | |
78 | }); | |
79 | ||
80 | /** | |
81 | * | |
82 | * options: | |
83 | * - ufGroupModel: UFGroupModel | |
84 | */ | |
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); | |
89 | }, | |
90 | ||
91 | /** | |
92 | * Look up a palette-field | |
93 | * | |
94 | * @param entityName | |
95 | * @param fieldName | |
96 | * @return {CRM.Designer.PaletteFieldModel} | |
97 | */ | |
98 | getFieldByName: function(entityName, fieldName) { | |
99 | return this.find(function(paletteFieldModel) { | |
0b1bae4a | 100 | return ((!entityName || paletteFieldModel.get('entityName') == entityName) && paletteFieldModel.get('fieldName') == fieldName); |
6a488035 TO |
101 | }); |
102 | }, | |
103 | ||
104 | /** | |
105 | * Get a list of all fields, grouped into sections by "entityName+sectionName". | |
106 | * | |
107 | * @return {Object} keys are sections ("entityName+sectionName"); values are CRM.Designer.PaletteFieldModel | |
108 | */ | |
109 | getFieldsByEntitySection: function() { | |
110 | // TODO cache | |
111 | var fieldsByEntitySection = this.groupBy(function(paletteFieldModel) { | |
112 | return paletteFieldModel.get('entityName') + '-' + paletteFieldModel.getSectionName(); | |
113 | }); | |
114 | return fieldsByEntitySection; | |
115 | } | |
116 | }); | |
117 | })(cj); |