| 1 | (function($, _) { |
| 2 | var ufGroupCollection = new CRM.UF.UFGroupCollection(_.sortBy(CRM.initialProfileList.values, 'title')); |
| 3 | //var ufGroupCollection = new CRM.UF.UFGroupCollection(CRM.initialProfileList.values, { |
| 4 | // comparator: 'title' // no point, this doesn't work with subcollections |
| 5 | //}); |
| 6 | ufGroupCollection.unshift(new CRM.UF.UFGroupModel({ |
| 7 | id: '', |
| 8 | title: ts('- select -') |
| 9 | })); |
| 10 | |
| 11 | /** |
| 12 | * Example: |
| 13 | * <input type="text" value="{$profileId}" class="crm-profile-selector" /> |
| 14 | * ... |
| 15 | * cj('.crm-profile-selector').crmProfileSelector({ |
| 16 | * groupTypeFilter: "Contact,Individual,Activity;;ActivityType:7", |
| 17 | * entities: "contact_1:IndividualModel,activity_1:ActivityModel" |
| 18 | * }); |
| 19 | * |
| 20 | * Note: The system does not currently support dynamic entities -- it only supports |
| 21 | * a couple of entities named "contact_1" and "activity_1". See also |
| 22 | * CRM.UF.guessEntityName(). |
| 23 | */ |
| 24 | $.fn.crmProfileSelector = function(options) { |
| 25 | return this.each(function() { |
| 26 | // Hide the existing <SELECT> and instead construct a ProfileSelector view. |
| 27 | // Keep them synchronized. |
| 28 | var select = this; |
| 29 | |
| 30 | var matchingUfGroups; |
| 31 | if (options.groupTypeFilter) { |
| 32 | matchingUfGroups = ufGroupCollection.subcollection({ |
| 33 | filter: function(ufGroupModel) { |
| 34 | return ufGroupModel.checkGroupType(options.groupTypeFilter); |
| 35 | } |
| 36 | }); |
| 37 | } else { |
| 38 | matchingUfGroups = ufGroupCollection; |
| 39 | } |
| 40 | |
| 41 | var view = new CRM.ProfileSelector.View({ |
| 42 | ufGroupId: $(select).val(), |
| 43 | ufGroupCollection: matchingUfGroups, |
| 44 | ufEntities: options.entities |
| 45 | }); |
| 46 | view.on('change:ufGroupId', function() { |
| 47 | $(select).val(view.getUfGroupId()).change(); |
| 48 | }); |
| 49 | view.render(); |
| 50 | $(select).after(view.el); |
| 51 | setTimeout(function() { |
| 52 | view.doPreview(); |
| 53 | }, 100); |
| 54 | }); |
| 55 | }; |
| 56 | |
| 57 | $('#crm-container').on('crmLoad', function() { |
| 58 | $('.crm-profile-selector:not(.rendered)', this).addClass('rendered').each(function() { |
| 59 | $(this).crmProfileSelector({ |
| 60 | groupTypeFilter: $(this).attr('data-group-type'), |
| 61 | entities: $(this).data('entities') |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | })(CRM.$, CRM._); |