Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-01-20-19-00-14
[civicrm-core.git] / js / model / crm.uf.js
1 (function($) {
2 var CRM = (window.CRM) ? (window.CRM) : (window.CRM = {});
3 if (!CRM.UF) CRM.UF = {};
4
5 var YESNO = [
6 {val: 0, label: ts('No')},
7 {val: 1, label: ts('Yes')}
8 ];
9
10 var VISIBILITY = [
11 {val: 'User and User Admin Only', label: ts('User and User Admin Only'), isInSelectorAllowed: false},
12 {val: 'Public Pages', label: ts('Public Pages'), isInSelectorAllowed: true},
13 {val: 'Public Pages and Listings', label: ts('Public Pages and Listings'), isInSelectorAllowed: true}
14 ];
15
16 var LOCATION_TYPES = _.map(CRM.PseudoConstant.locationType, function(value, key) {
17 return {val: key, label: value};
18 });
19 LOCATION_TYPES.unshift({val: '', label: ts('Primary')});
20 var DEFAULT_LOCATION_TYPE_ID = '';
21
22 var PHONE_TYPES = _.map(CRM.PseudoConstant.phoneType, function(value, key) {
23 return {val: key, label: value};
24 });
25 var DEFAULT_PHONE_TYPE_ID = PHONE_TYPES[0].val;
26
27 /**
28 * Add a help link to a form label
29 */
30 function addHelp(title, options) {
31 return title + ' <a href="#" onclick=\'CRM.help("' + title + '", ' + JSON.stringify(options) + '); return false;\' title="' + ts('%1 Help', {1: title}) + '" class="helpicon"></a>';
32 }
33
34 function watchChanges() {
35 CRM.designerApp.vent.trigger('ufUnsaved', true);
36 }
37
38 /**
39 * Parse a "group_type" expression
40 *
41 * @param string groupTypeExpr example: "Individual,Activity\0ActivityType:2:28"
42 * Note: I've seen problems where HTML "&#00;" != JS '\0', so we support ';;' as an equivalent delimiter
43 * @return Object example: {coreTypes: {"Individual":true,"Activity":true}, subTypes: {"ActivityType":{2: true, 28:true}]}}
44 */
45 CRM.UF.parseTypeList = function(groupTypeExpr) {
46 var typeList = {coreTypes: {}, subTypes:{}};
47 // The API may have automatically converted a string with '\0' to an array
48 var parts = _.isArray(groupTypeExpr) ? groupTypeExpr : groupTypeExpr.replace(';;','\0').split('\0');
49 var coreTypesExpr = parts[0];
50 var subTypesExpr = parts[1];
51
52 if (coreTypesExpr && coreTypesExpr != '') {
53 _.each(coreTypesExpr.split(','), function(coreType){
54 typeList.coreTypes[coreType] = true;
55 });
56 }
57
58 if (subTypesExpr && subTypesExpr != '') {
59 var subTypes = subTypesExpr.split(':');
60 var subTypeKey = subTypes.shift();
61 typeList.subTypes[subTypeKey] = {};
62 _.each(subTypes, function(subTypeId){
63 typeList.subTypes[subTypeKey][subTypeId] = true;
64 });
65 }
66 return typeList;
67 };
68
69 /**
70 * This function is a hack for generating simulated values of "entity_name"
71 * in the form-field model.
72 *
73 * @param {string} field_type
74 * @return {string}
75 */
76 CRM.UF.guessEntityName = function(field_type) {
77 switch (field_type) {
78 case 'Contact':
79 case 'Individual':
80 return 'contact_1';
81 case 'Organization':
82 return 'organization_1';
83 case 'Household':
84 return 'household_1';
85 case 'Activity':
86 return 'activity_1';
87 case 'Contribution':
88 return 'contribution_1';
89 case 'Membership':
90 return 'membership_1';
91 case 'Participant':
92 return 'participant_1';
93 default:
94 throw "Cannot guess entity name for field_type=" + field_type;
95 }
96 }
97
98 /**
99 * Represents a field in a customizable form.
100 */
101 CRM.UF.UFFieldModel = CRM.Backbone.Model.extend({
102 /**
103 * Backbone.Form descripton of the field to which this refers
104 */
105 defaults: {
106 help_pre: '',
107 help_post: '',
108 /**
109 * @var bool, non-persistent indication of whether this field is unique or duplicate
110 * within its UFFieldCollection
111 */
112 is_duplicate: false
113 },
114 schema: {
115 'id': {
116 type: 'Number'
117 },
118 'uf_group_id': {
119 type: 'Number'
120 },
121 'entity_name': {
122 // pseudo-field
123 type: 'Text'
124 },
125 'field_name': {
126 type: 'Text'
127 },
128 'field_type': {
129 type: 'Select',
130 options: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
131 },
132 'help_post': {
133 title: addHelp(ts('Field Post Help'), {id: "help", file:"CRM/UF/Form/Field"}),
134 type: 'TextArea'
135 },
136 'help_pre': {
137 title: addHelp(ts('Field Pre Help'), {id: "help", file:"CRM/UF/Form/Field"}),
138 type: 'TextArea'
139 },
140 'in_selector': {
141 title: addHelp(ts('Results Columns?'), {id: "in_selector", file:"CRM/UF/Form/Field"}),
142 type: 'Select',
143 options: YESNO
144 },
145 'is_active': {
146 title: addHelp(ts('Active?'), {id: "is_active", file:"CRM/UF/Form/Field"}),
147 type: 'Select',
148 options: YESNO
149 },
150 'is_multi_summary': {
151 title: ts("Include in multi-record listing?"),
152 type: 'Select',
153 options: YESNO
154 },
155 'is_required': {
156 title: addHelp(ts('Required?'), {id: "is_required", file:"CRM/UF/Form/Field"}),
157 type: 'Select',
158 options: YESNO
159 },
160 'is_reserved': {
161 type: 'Select',
162 options: YESNO
163 },
164 'is_searchable': {
165 title: addHelp(ts("Searchable"), {id: "is_searchable", file:"CRM/UF/Form/Field"}),
166 type: 'Select',
167 options: YESNO
168 },
169 'is_view': {
170 title: addHelp(ts('View Only?'), {id: "is_view", file:"CRM/UF/Form/Field"}),
171 type: 'Select',
172 options: YESNO
173 },
174 'label': {
175 title: ts('Field Label'),
176 type: 'Text'
177 },
178 'location_type_id': {
179 title: ts('Location Type'),
180 type: 'Select',
181 options: LOCATION_TYPES
182 },
183 'phone_type_id': {
184 title: ts('Phone Type'),
185 type: 'Select',
186 options: PHONE_TYPES
187 },
188 'visibility': {
189 title: addHelp(ts('Visibility'), {id: "visibility", file:"CRM/UF/Form/Field"}),
190 type: 'Select',
191 options: VISIBILITY
192 },
193 'weight': {
194 type: 'Number'
195 }
196 },
197 initialize: function() {
198 this.set('entity_name', CRM.UF.guessEntityName(this.get('field_type')));
199 this.on("rel:ufGroupModel", this.applyDefaults, this);
200 this.on('change', watchChanges);
201 },
202 applyDefaults: function() {
203 var fieldSchema = this.getFieldSchema();
204 if (fieldSchema && fieldSchema.civiIsLocation && !this.get('location_type_id')) {
205 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID);
206 }
207 if (fieldSchema && fieldSchema.civiIsPhone && !this.get('phone_type_id')) {
208 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID);
209 }
210 },
211 isInSelectorAllowed: function() {
212 var visibility = _.first(_.where(VISIBILITY, {val: this.get('visibility')}));
213 return visibility.isInSelectorAllowed;
214 },
215 getFieldSchema: function() {
216 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
217 },
218 /**
219 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
220 * have a unique signature.
221 *
222 * @return {String}
223 */
224 getSignature: function() {
225 return this.get("entity_name")
226 + '::' + this.get("field_name")
227 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
228 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
229 },
230
231 /**
232 * This is like destroy(), but it only destroys the item on the client-side;
233 * it does not trigger REST or Backbone.sync() operations.
234 *
235 * @return {Boolean}
236 */
237 destroyLocal: function() {
238 this.trigger('destroy', this, this.collection, {});
239 return false;
240 }
241 });
242
243 /**
244 * Represents a list of fields in a customizable form
245 *
246 * options:
247 * - uf_group_id: int
248 */
249 CRM.UF.UFFieldCollection = CRM.Backbone.Collection.extend({
250 model: CRM.UF.UFFieldModel,
251 uf_group_id: null, // int
252 initialize: function(models, options) {
253 options = options || {};
254 this.uf_group_id = options.uf_group_id;
255 this.initializeCopyToChildrenRelation('ufGroupModel', options.ufGroupModel, models);
256 this.on('add', this.watchDuplicates, this);
257 this.on('remove', this.unwatchDuplicates, this);
258 this.on('change', watchChanges);
259 this.on('add', watchChanges);
260 this.on('remove', watchChanges);
261 },
262 getFieldsByName: function(entityName, fieldName) {
263 return this.filter(function(ufFieldModel) {
264 return (ufFieldModel.get('entity_name') == entityName && ufFieldModel.get('field_name') == fieldName);
265 });
266 },
267 toSortedJSON: function() {
268 var fields = this.map(function(ufFieldModel){
269 return ufFieldModel.toStrictJSON();
270 });
271 return _.sortBy(fields, function(ufFieldJSON){
272 return parseInt(ufFieldJSON.weight);
273 });
274 },
275 isAddable: function(ufFieldModel) {
276 var entity_name = ufFieldModel.get('entity_name'),
277 field_name = ufFieldModel.get('field_name'),
278 fieldSchema = this.getRel('ufGroupModel').getFieldSchema(ufFieldModel.get('entity_name'), ufFieldModel.get('field_name'));
279
280 if (! fieldSchema) {
281 return false;
282 }
283 var fields = this.getFieldsByName(entity_name, field_name);
284 var limit = 1;
285 if (fieldSchema.civiIsLocation) {
286 limit *= LOCATION_TYPES.length;
287 }
288 if (fieldSchema.civiIsPhone) {
289 limit *= PHONE_TYPES.length;
290 }
291 return fields.length < limit;
292 },
293 watchDuplicates: function(model, collection, options) {
294 model.on('change:location_type_id', this.markDuplicates, this);
295 model.on('change:phone_type_id', this.markDuplicates, this);
296 this.markDuplicates();
297 },
298 unwatchDuplicates: function(model, collection, options) {
299 model.off('change:location_type_id', this.markDuplicates, this);
300 model.off('change:phone_type_id', this.markDuplicates, this);
301 this.markDuplicates();
302 },
303 hasDuplicates: function() {
304 var firstDupe = this.find(function(ufFieldModel){
305 return ufFieldModel.get('is_duplicate');
306 });
307 return firstDupe ? true : false;
308 },
309 /**
310 *
311 */
312 markDuplicates: function() {
313 var ufFieldModelsByKey = this.groupBy(function(ufFieldModel) {
314 return ufFieldModel.getSignature();
315 });
316 this.each(function(ufFieldModel){
317 var is_duplicate = ufFieldModelsByKey[ufFieldModel.getSignature()].length > 1;
318 if (is_duplicate != ufFieldModel.get('is_duplicate')) {
319 ufFieldModel.set('is_duplicate', is_duplicate);
320 }
321 });
322 }
323 });
324
325 /**
326 * Represents an entity in a customizable form
327 */
328 CRM.UF.UFEntityModel = CRM.Backbone.Model.extend({
329 schema: {
330 'id': {
331 // title: ts(''),
332 type: 'Number'
333 },
334 'entity_name': {
335 title: ts('Entity Name'),
336 help: ts('Symbolic name which referenced in the fields'),
337 type: 'Text'
338 },
339 'entity_type': {
340 title: ts('Entity Type'),
341 type: 'Select',
342 options: ['IndividualModel', 'ActivityModel']
343 },
344 'entity_sub_type': {
345 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
346 title: ts('Sub Type'),
347 type: 'Text'
348 }
349 },
350 defaults: {
351 entity_sub_type: '*'
352 },
353 initialize: function() {
354 },
355 /**
356 * Get a list of all fields that can be used with this entity.
357 *
358 * @return {Object} keys are field names; values are fieldSchemas
359 */
360 getFieldSchemas: function() {
361 var ufEntityModel = this;
362 var modelClass= this.getModelClass();
363
364 if (this.get('entity_sub_type') == '*') {
365 return _.clone(modelClass.prototype.schema);
366 }
367
368 var result = {};
369 _.each(modelClass.prototype.schema, function(fieldSchema, fieldName){
370 var section = modelClass.prototype.sections[fieldSchema.section];
371 if (ufEntityModel.isSectionEnabled(section)) {
372 result[fieldName] = fieldSchema;
373 }
374 });
375 return result;
376 },
377 isSectionEnabled: function(section) {
378 return (!section || !section.extends_entity_column_value || _.contains(section.extends_entity_column_value, this.get('entity_sub_type')));
379 },
380 getSections: function() {
381 var ufEntityModel = this;
382 var result = {};
383 _.each(ufEntityModel.getModelClass().prototype.sections, function(section, sectionKey){
384 if (ufEntityModel.isSectionEnabled(section)) {
385 result[sectionKey] = section;
386 }
387 });
388 return result;
389 },
390 getModelClass: function() {
391 return CRM.Schema[this.get('entity_type')];
392 }
393 });
394
395 /**
396 * Represents a list of entities in a customizable form
397 *
398 * options:
399 * - ufGroupModel: UFGroupModel
400 */
401 CRM.UF.UFEntityCollection = CRM.Backbone.Collection.extend({
402 model: CRM.UF.UFEntityModel,
403 byName: {},
404 initialize: function(models, options) {
405 options = options || {};
406 this.initializeCopyToChildrenRelation('ufGroupModel', options.ufGroupModel, models);
407 },
408 /**
409 *
410 * @param name
411 * @return {UFEntityModel} if found; otherwise, null
412 */
413 getByName: function(name) {
414 // TODO consider indexing
415 return this.find(function(ufEntityModel){
416 return ufEntityModel.get('entity_name') == name;
417 });
418 }
419 });
420
421 /**
422 * Represents a customizable form
423 */
424 CRM.UF.UFGroupModel = CRM.Backbone.Model.extend({
425 defaults: {
426 title: ts('Unnamed Profile'),
427 is_active: 1
428 },
429 schema: {
430 'id': {
431 // title: ts(''),
432 type: 'Number'
433 },
434 'name': {
435 // title: ts(''),
436 type: 'Text'
437 },
438 'title': {
439 title: ts('Profile Name'),
440 help: ts(''),
441 type: 'Text',
442 validators: ['required']
443 },
444 'group_type': {
445 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
446 // title: ts(''),
447 type: 'Text'
448 },
449 'add_captcha': {
450 title: ts('Include reCAPTCHA?'),
451 help: ts('FIXME'),
452 type: 'Select',
453 options: YESNO
454 },
455 'add_to_group_id': {
456 title: ts('Add new contacts to a Group?'),
457 help: ts('Select a group if you are using this profile for adding new contacts, AND you want the new contacts to be automatically assigned to a group.'),
458 type: 'Number'
459 },
460 'cancel_URL': {
461 title: ts('Cancel Redirect URL'),
462 help: ts('If you are using this profile as a contact signup or edit form, and want to redirect the user to a static URL if they click the Cancel button - enter the complete URL here. If this field is left blank, the built-in Profile form will be redisplayed.'),
463 type: 'Text'
464 },
465 'created_date': {
466 //title: ts(''),
467 type: 'Text'// FIXME
468 },
469 'created_id': {
470 //title: ts(''),
471 type: 'Number'
472 },
473 'help_post': {
474 title: ts('Post-form Help'),
475 help: ts('Explanatory text displayed at the end of the form.')
476 + ts('Note that this help text is displayed on profile create/edit screens only.'),
477 type: 'TextArea'
478 },
479 'help_pre': {
480 title: ts('Pre-form Help '),
481 help: ts('Explanatory text displayed at the beginning of the form.')
482 + ts('Note that this help text is displayed on profile create/edit screens only.'),
483 type: 'TextArea'
484 },
485 'is_active': {
486 title: ts('Is this CiviCRM Profile active?'),
487 type: 'Select',
488 options: YESNO
489 },
490 'is_cms_user': {
491 title: ts('Drupal user account registration option?'),// FIXME
492 help: ts('FIXME'),
493 type: 'Select',
494 options: YESNO // FIXME
495 },
496 'is_edit_link': {
497 title: ts('Include profile edit links in search results?'),
498 help: ts('Check this box if you want to include a link in the listings to Edit profile fields. Only users with permission to edit the contact will see this link.'),
499 type: 'Select',
500 options: YESNO
501 },
502 'is_map': {
503 title: ts('Enable mapping for this profile?'),
504 help: ts('If enabled, a Map link is included on the profile listings rows and detail screens for any contacts whose records include sufficient location data for your mapping provider.'),
505 type: 'Select',
506 options: YESNO
507 },
508 'is_proximity_search': {
509 title: ts('Proximity search'),
510 help: ts('FIXME'),
511 type: 'Select',
512 options: YESNO // FIXME
513 },
514 'is_reserved': {
515 // title: ts(''),
516 type: 'Select',
517 options: YESNO
518 },
519 'is_uf_link': {
520 title: ts('Include Drupal user account information links in search results?'), // FIXME
521 help: ts('FIXME'),
522 type: 'Select',
523 options: YESNO
524 },
525 'is_update_dupe': {
526 title: ts('What to do upon duplicate match'),
527 help: ts('FIXME'),
528 type: 'Select',
529 options: YESNO // FIXME
530 },
531 'limit_listings_group_id': {
532 title: ts('Limit listings to a specific Group?'),
533 help: ts('Select a group if you are using this profile for search and listings, AND you want to limit the listings to members of a specific group.'),
534 type: 'Number'
535 },
536 'notify': {
537 title: ts('Notify when profile form is submitted?'),
538 help: ts('If you want member(s) of your organization to receive a notification email whenever this Profile form is used to enter or update contact information, enter one or more email addresses here. Multiple email addresses should be separated by a comma (e.g. jane@example.org, paula@example.org). The first email address listed will be used as the FROM address in the notifications.'),
539 type: 'TextArea'
540 },
541 'post_URL': {
542 title: ts('Redirect URL'),
543 help: ts("If you are using this profile as a contact signup or edit form, and want to redirect the user to a static URL after they've submitted the form, you can also use contact tokens in URL - enter the complete URL here. If this field is left blank, the built-in Profile form will be redisplayed with a generic status message - 'Your contact information has been saved.'"),
544 type: 'Text'
545 },
546 'weight': {
547 title: ts('Order'),
548 help: ts('Weight controls the order in which profiles are presented when more than one profile is included in User Registration or My Account screens. Enter a positive or negative integer - lower numbers are displayed ahead of higher numbers.'),
549 type: 'Number'
550 // FIXME positive int
551 }
552 },
553 initialize: function() {
554 var ufGroupModel = this;
555
556 if (!this.getRel('ufEntityCollection')) {
557 var ufEntityCollection = new CRM.UF.UFEntityCollection([], {
558 ufGroupModel: this,
559 silent: false
560 });
561 this.setRel('ufEntityCollection', ufEntityCollection);
562 }
563
564 if (!this.getRel('ufFieldCollection')) {
565 var ufFieldCollection = new CRM.UF.UFFieldCollection([], {
566 uf_group_id: this.id,
567 ufGroupModel: this
568 });
569 this.setRel('ufFieldCollection', ufFieldCollection);
570 }
571
572 if (!this.getRel('paletteFieldCollection')) {
573 var paletteFieldCollection = new CRM.Designer.PaletteFieldCollection([], {
574 ufGroupModel: this
575 });
576 paletteFieldCollection.sync = function(method, model, options) {
577 options || (options = {});
578 // console.log(method, model, options);
579 switch (method) {
580 case 'read':
581 var success = options.success;
582 options.success = function(resp, status, xhr) {
583 if (success) success(resp, status, xhr);
584 model.trigger('sync', model, resp, options);
585 };
586 success(ufGroupModel.buildPaletteFields());
587
588 break;
589 case 'create':
590 case 'update':
591 case 'delete':
592 default:
593 throw 'Unsupported method: ' + method;
594 }
595 };
596 this.setRel('paletteFieldCollection', paletteFieldCollection);
597 }
598
599 this.getRel('ufEntityCollection').on('reset', this.resetEntities, this);
600 this.resetEntities();
601
602 this.on('change', watchChanges);
603 },
604 /**
605 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
606 * is suitable for a new, identical UFGroup.
607 *
608 * @return {CRM.UF.UFGroupModel}
609 */
610 deepCopy: function() {
611 var copy = new CRM.UF.UFGroupModel(_.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
612 copy.getRel('ufEntityCollection').reset(
613 this.getRel('ufEntityCollection').toJSON()
614 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
615 );
616 copy.getRel('ufFieldCollection').reset(
617 this.getRel('ufFieldCollection').map(function(ufFieldModel) {
618 return _.omit(ufFieldModel.toStrictJSON(), ['id', 'uf_group_id']);
619 })
620 );
621 copy.set('title', ts('%1 (Copy)', {
622 1: copy.get('title')
623 }));
624 return copy;
625 },
626 getModelClass: function(entity_name) {
627 var ufEntity = this.getRel('ufEntityCollection').getByName(entity_name);
628 if (!ufEntity) throw 'Failed to locate entity: ' + entity_name;
629 return ufEntity.getModelClass();
630 },
631 getFieldSchema: function(entity_name, field_name) {
632 var modelClass = this.getModelClass(entity_name);
633 var fieldSchema = modelClass.prototype.schema[field_name];
634 if (!fieldSchema) {
635 if (console.log) {
636 console.log('Failed to locate field: ' + entity_name + "." + field_name);
637 }
638 return null;
639 }
640 return fieldSchema;
641 },
642 /**
643 * Check that the group_type contains *only* the types listed in validTypes
644 *
645 * @param string validTypesExpr
646 * @return {Boolean}
647 */
648 checkGroupType: function(validTypesExpr) {
649 var allMatched = true;
650 if (! this.get('group_type') || this.get('group_type') == '') {
651 return true;
652 }
653
654 var actualTypes = CRM.UF.parseTypeList(this.get('group_type'));
655 var validTypes = CRM.UF.parseTypeList(validTypesExpr);
656
657 // Every actual.coreType is a valid.coreType
658 _.each(actualTypes.coreTypes, function(ignore, actualCoreType) {
659 if (! validTypes.coreTypes[actualCoreType]) {
660 allMatched = false;
661 }
662 });
663
664 // Every actual.subType is a valid.subType
665 _.each(actualTypes.subTypes, function(actualSubTypeIds, actualSubTypeKey) {
666 if (!validTypes.subTypes[actualSubTypeKey]) {
667 allMatched = false;
668 return;
669 }
670 // actualSubTypeIds is a list of all subtypes which can be used by group,
671 // so it's sufficient to match any one of them
672 var subTypeMatched = false;
673 _.each(actualSubTypeIds, function(ignore, actualSubTypeId) {
674 if (validTypes.subTypes[actualSubTypeKey][actualSubTypeId]) {
675 subTypeMatched = true;
676 }
677 });
678 allMatched = allMatched && subTypeMatched;
679 });
680 return allMatched;
681 },
682 resetEntities: function() {
683 var ufGroupModel = this;
684 ufGroupModel.getRel('ufFieldCollection').each(function(ufFieldModel){
685 if (!ufFieldModel.getFieldSchema()) {
686 CRM.alert(ts('The data model no longer includes field "%1"! All references to the field have been removed.', {
687 1: ufFieldModel.get('entity_name') + "." + ufFieldModel.get('field_name')
688 }), '', 'alert', {expires: false});
689 ufFieldModel.destroyLocal();
690 }
691 });
692 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
693 },
694 /**
695 *
696 * @return {Array} of PaletteFieldModel
697 */
698 buildPaletteFields: function() {
699 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
700 // as appropriate
701 // Note: The system as a whole is ill-defined in cases where we have an existing
702 // UFField that references a model field that disappears.
703
704 var ufGroupModel = this;
705
706 var oldPaletteFieldModelsBySig = {};
707 this.getRel('paletteFieldCollection').each(function(paletteFieldModel){
708 oldPaletteFieldModelsBySig[paletteFieldModel.get("entityName") + '::' + paletteFieldModel.get("fieldName")] = paletteFieldModel;
709 });
710
711 var newPaletteFieldModels = [];
712 this.getRel('ufEntityCollection').each(function(ufEntityModel){
713 var modelClass = ufEntityModel.getModelClass();
714 _.each(ufEntityModel.getFieldSchemas(), function(value, key, list) {
715 var model = oldPaletteFieldModelsBySig[ufEntityModel.get('entity_name') + '::' + key];
716 if (!model) {
717 model = new CRM.Designer.PaletteFieldModel({
718 modelClass: modelClass,
719 entityName: ufEntityModel.get('entity_name'),
720 fieldName: key
721 });
722 }
723 newPaletteFieldModels.push(model);
724 });
725 });
726
727 return newPaletteFieldModels;
728 }
729 });
730
731 /**
732 * Represents a list of customizable form
733 */
734 CRM.UF.UFGroupCollection = CRM.Backbone.Collection.extend({
735 model: CRM.UF.UFGroupModel
736 });
737 })(cj);