2 if (!CRM
.ProfileSelector
) CRM
.ProfileSelector
= {};
4 CRM
.ProfileSelector
.Option
= Backbone
.Marionette
.ItemView
.extend({
5 template
: '#profile_selector_option_template',
8 'change:title': 'render'
10 onRender: function() {
11 this.$el
.attr('value', this.model
.get('id'));
15 CRM
.ProfileSelector
.Select
= Backbone
.Marionette
.CollectionView
.extend({
17 itemView
: CRM
.ProfileSelector
.Option
21 * Render a pane with 'Select/Preview/Edit/Copy/Create' functionality for profiles.
23 * Note: This view works with a ufGroupCollection, and it creates popups for a
24 * ufGroupModel. These are related but not facilely. The ufGroupModels in the
25 * ufGroupCollection are never passed to the popup, and the models from the
26 * popup are never added to the collection. This is because the popup works
27 * with temporary, local copies -- but the collection reflects the actual list
31 * - ufGroupId: int, the default selection
32 * - ufGroupCollection: the profiles which can be selected
33 * - ufEntities: hard-coded entity list used with any new/existing forms
34 * (this may be removed when the form-runtime is updated to support hand-picking
35 * entities for each form)
37 CRM
.ProfileSelector
.View
= Backbone
.Marionette
.Layout
.extend({
38 template
: '#profile_selector_template',
40 selectRegion
: '.crm-profile-selector-select'
43 'change .crm-profile-selector-select select': 'onChangeUfGroupId',
44 'click .crm-profile-selector-edit': 'doEdit',
45 'click .crm-profile-selector-copy': 'doCopy',
46 'click .crm-profile-selector-create': 'doCreate',
47 // prevent interaction with preview form
48 'click .crm-profile-selector-preview-pane': false,
49 'crmLoad .crm-profile-selector-preview-pane': 'disableForm'
51 /** @var Marionette.View which specifically builds on jQuery-UI's dialog */
53 onRender: function() {
54 var view
= new CRM
.ProfileSelector
.Select({
55 collection
: this.options
.ufGroupCollection
57 this.selectRegion
.show(view
);
58 this.setUfGroupId(this.options
.ufGroupId
, {silent
: true});
61 onChangeUfGroupId: function(event
) {
62 this.options
.ufGroupId
= $(event
.target
).val();
63 this.trigger('change:ufGroupId', this);
67 toggleButtons: function() {
68 this.$('.crm-profile-selector-edit,.crm-profile-selector-copy').prop('disabled', !this.hasUfGroupId());
70 hasUfGroupId: function() {
71 return (this.getUfGroupId() && this.getUfGroupId() != '') ? true : false;
73 setUfGroupId: function(value
, options
) {
74 this.options
.ufGroupId
= value
;
75 this.$('.crm-profile-selector-select select').val(value
);
76 if (!options
|| !options
.silent
) {
77 this.$('.crm-profile-selector-select select').change();
80 getUfGroupId: function() {
81 return this.options
.ufGroupId
;
83 doPreview: function() {
84 var $pane
= this.$('.crm-profile-selector-preview-pane');
85 if (!this.hasUfGroupId()) {
86 $pane
.html($('#profile_selector_empty_preview_template').html());
88 CRM
.loadPage(CRM
.url("civicrm/ajax/inline", {class_name
: 'CRM_UF_Form_Inline_PreviewById', id
: this.getUfGroupId()}), {target
: $pane
});
91 disableForm: function() {
92 this.$(':input', '.crm-profile-selector-preview-pane').prop('readOnly', true);
96 var profileSelectorView
= this;
97 var designerDialog
= new CRM
.Designer
.DesignerDialog({
98 findCreateUfGroupModel: function(options
) {
99 var ufId
= profileSelectorView
.getUfGroupId();
100 // Retrieve UF group and fields from the api
101 CRM
.api('UFGroup', 'getsingle', {id
: ufId
, "api.UFField.get": 1}, {
102 success: function(formData
) {
103 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
104 var ufGroupModel
= new CRM
.UF
.UFGroupModel(_
.pick(formData
, _
.keys(CRM
.UF
.UFGroupModel
.prototype.schema
)));
105 ufGroupModel
.setUFGroupModel(ufGroupModel
.calculateContactEntityType(), profileSelectorView
.options
.ufEntities
);
106 ufGroupModel
.getRel('ufFieldCollection').reset(_
.values(formData
["api.UFField.get"].values
));
107 options
.onLoad(ufGroupModel
);
112 CRM
.designerApp
.vent
.on('ufSaved', this.onSave
, this);
113 this.setDialog(designerDialog
);
115 doCopy: function(e
) {
117 // This is largely the same as doEdit, but we ultimately pass in a deepCopy of the ufGroupModel.
118 var profileSelectorView
= this;
119 var designerDialog
= new CRM
.Designer
.DesignerDialog({
120 findCreateUfGroupModel: function(options
) {
121 var ufId
= profileSelectorView
.getUfGroupId();
122 // Retrieve UF group and fields from the api
123 CRM
.api('UFGroup', 'getsingle', {id
: ufId
, "api.UFField.get": 1}, {
124 success: function(formData
) {
125 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
126 var ufGroupModel
= new CRM
.UF
.UFGroupModel(_
.pick(formData
, _
.keys(CRM
.UF
.UFGroupModel
.prototype.schema
)));
127 ufGroupModel
.setUFGroupModel(ufGroupModel
.calculateContactEntityType(), profileSelectorView
.options
.ufEntities
);
128 ufGroupModel
.getRel('ufFieldCollection').reset(_
.values(formData
["api.UFField.get"].values
));
129 options
.onLoad(ufGroupModel
.deepCopy());
134 CRM
.designerApp
.vent
.on('ufSaved', this.onSave
, this);
135 this.setDialog(designerDialog
);
137 doCreate: function(e
) {
139 var profileSelectorView
= this;
140 var designerDialog
= new CRM
.Designer
.DesignerDialog({
141 findCreateUfGroupModel: function(options
) {
142 // Initialize new UF group
143 var ufGroupModel
= new CRM
.UF
.UFGroupModel();
144 ufGroupModel
.getRel('ufEntityCollection').reset(profileSelectorView
.options
.ufEntities
);
145 options
.onLoad(ufGroupModel
);
148 CRM
.designerApp
.vent
.on('ufSaved', this.onSave
, this);
149 this.setDialog(designerDialog
);
152 CRM
.designerApp
.vent
.off('ufSaved', this.onSave
, this);
153 var ufGroupId
= this.activeDialog
.model
.get('id');
154 var modelFromCollection
= this.options
.ufGroupCollection
.get(ufGroupId
);
155 if (modelFromCollection
) {
156 // copy in changes to UFGroup
157 modelFromCollection
.set(this.activeDialog
.model
.toStrictJSON());
159 // add in new UFGroup
160 modelFromCollection
= new CRM
.UF
.UFGroupModel(this.activeDialog
.model
.toStrictJSON());
161 this.options
.ufGroupCollection
.add(modelFromCollection
);
163 this.setUfGroupId(ufGroupId
);
166 setDialog: function(view
) {
167 if (this.activeDialog
) {
168 this.activeDialog
.close();
170 this.activeDialog
= view
;