Merge pull request #13640 from pradpnayak/notevalidation
[civicrm-core.git] / js / model / crm.designer.js
CommitLineData
4b513f23 1(function($, _) {
6a488035
TO
2 if (!CRM.Designer) CRM.Designer = {};
3
4 // TODO Optimize this class
5 CRM.Designer.PaletteFieldModel = CRM.Backbone.Model.extend({
6 defaults: {
7 /**
8 * @var {string} required; a form-specific binding to an entity instance (eg 'student', 'mother')
9 */
10 entityName: null,
11
12 /**
13 * @var {string}
14 */
15 fieldName: null
16 },
17 initialize: function() {
18 },
19 getFieldSchema: function() {
20 return this.getRel('ufGroupModel').getFieldSchema(this.get('entityName'), this.get('fieldName'));
21 },
22 getLabel: function() {
23 // Note: if fieldSchema were a bit tighter, then we need to get a label from PaletteFieldModel at all
24 return this.getFieldSchema().title || this.get('fieldName');
25 },
26 getSectionName: function() {
27 // Note: if fieldSchema were a bit tighter, then we need to get a section from PaletteFieldModel at all
28 return this.getFieldSchema().section || 'default';
29 },
30 getSection: function() {
31 return this.getRel('ufGroupModel').getModelClass(this.get('entityName')).prototype.sections[this.getSectionName()];
32 },
33 /**
34 * Add a new UFField model to a UFFieldCollection (if doing so is legal).
35 * If it fails, display an alert.
36 *
37 * @param {int} ufGroupId
38 * @param {CRM.UF.UFFieldCollection} ufFieldCollection
39 * @param {Object} addOptions
40 * @return {CRM.UF.UFFieldModel} or null (if the field is not addable)
41 */
42 addToUFCollection: function(ufFieldCollection, addOptions) {
8fd0e719 43 var name, paletteFieldModel = this;
6a488035
TO
44 var ufFieldModel = paletteFieldModel.createUFFieldModel(ufFieldCollection.getRel('ufGroupModel'));
45 ufFieldModel.set('uf_group_id', ufFieldCollection.uf_group_id);
46 if (!ufFieldCollection.isAddable(ufFieldModel)) {
47 CRM.alert(
48 ts('The field "%1" is already included.', {
49 1: paletteFieldModel.getLabel()
50 }),
51 ts('Duplicate'),
52 'alert'
53 );
54 return null;
55 }
56 ufFieldCollection.add(ufFieldModel, addOptions);
8fd0e719
CW
57 // Load metadata and set defaults
58 // TODO: currently only works for custom fields
59 name = this.get('fieldName').split('_');
60 if (name[0] === 'custom') {
61 CRM.api('custom_field', 'getsingle', {id: name[1]}, {success: function(field) {
ed619003 62 ufFieldModel.set(_.pick(field, 'help_pre', 'help_post', 'is_required'));
8fd0e719
CW
63 }});
64 }
6a488035
TO
65 return ufFieldModel;
66 },
67 createUFFieldModel: function(ufGroupModel) {
68 var model = new CRM.UF.UFFieldModel({
69 is_active: 1,
70 label: this.getLabel(),
71 entity_name: this.get('entityName'),
72 field_type: this.getFieldSchema().civiFieldType,
0103eaf0
CW
73 // For some reason the 'formatting' field gets a random number appended in core so we mimic that here.
74 // TODO: Why?
75 field_name: this.get('fieldName') == 'formatting' ? 'formatting_' + (Math.floor(Math.random() * 8999) + 1000) : this.get('fieldName')
6a488035
TO
76 });
77 return model;
78 }
79 });
80
81 /**
82 *
83 * options:
84 * - ufGroupModel: UFGroupModel
85 */
86 CRM.Designer.PaletteFieldCollection = CRM.Backbone.Collection.extend({
87 model: CRM.Designer.PaletteFieldModel,
88 initialize: function(models, options) {
89 this.initializeCopyToChildrenRelation('ufGroupModel', options.ufGroupModel, models);
90 },
91
92 /**
93 * Look up a palette-field
94 *
95 * @param entityName
96 * @param fieldName
97 * @return {CRM.Designer.PaletteFieldModel}
98 */
99 getFieldByName: function(entityName, fieldName) {
0103eaf0
CW
100 if (fieldName.indexOf('formatting') === 0) {
101 fieldName = 'formatting';
102 }
6a488035 103 return this.find(function(paletteFieldModel) {
0b1bae4a 104 return ((!entityName || paletteFieldModel.get('entityName') == entityName) && paletteFieldModel.get('fieldName') == fieldName);
6a488035
TO
105 });
106 },
107
108 /**
109 * Get a list of all fields, grouped into sections by "entityName+sectionName".
110 *
111 * @return {Object} keys are sections ("entityName+sectionName"); values are CRM.Designer.PaletteFieldModel
112 */
113 getFieldsByEntitySection: function() {
114 // TODO cache
115 var fieldsByEntitySection = this.groupBy(function(paletteFieldModel) {
116 return paletteFieldModel.get('entityName') + '-' + paletteFieldModel.getSectionName();
117 });
118 return fieldsByEntitySection;
119 }
120 });
4b513f23 121})(CRM.$, CRM._);