2 var CRM
= (window
.CRM
) ? (window
.CRM
) : (window
.CRM
= {});
3 if (!CRM
.ProfileSelector
) CRM
.ProfileSelector
= {};
5 CRM
.ProfileSelector
.Option
= Backbone
.Marionette
.ItemView
.extend({
6 template
: '#profile_selector_option_template',
9 'change:title': 'render'
11 onRender: function() {
12 this.$el
.attr('value', this.model
.get('id'));
16 CRM
.ProfileSelector
.Select
= Backbone
.Marionette
.CollectionView
.extend({
18 itemView
: CRM
.ProfileSelector
.Option
22 * Render a pane with 'Select/Preview/Edit/Copy/Create' functionality for profiles.
24 * Note: This view works with a ufGroupCollection, and it creates popups for a
25 * ufGroupModel. These are related but not facilely. The ufGroupModels in the
26 * ufGroupCollection are never passed to the popup, and the models from the
27 * popup are never added to the collection. This is because the popup works
28 * with temporary, local copies -- but the collection reflects the actual list
32 * - ufGroupId: int, the default selection
33 * - ufGroupCollection: the profiles which can be selected
34 * - ufEntities: hard-coded entity list used with any new/existing forms
35 * (this may be removed when the form-runtime is updated to support hand-picking
36 * entities for each form)
38 CRM
.ProfileSelector
.View
= Backbone
.Marionette
.Layout
.extend({
39 template
: '#profile_selector_template',
41 selectRegion
: '.crm-profile-selector-select'
44 'change .crm-profile-selector-select select': 'onChangeUfGroupId',
45 'click .crm-profile-selector-edit': 'doEdit',
46 'click .crm-profile-selector-copy': 'doCopy',
47 'click .crm-profile-selector-create': 'doCreate'
49 /** @var Marionette.View which specifically builds on jQuery-UI's dialog */
51 onRender: function() {
52 var view
= new CRM
.ProfileSelector
.Select({
53 collection
: this.options
.ufGroupCollection
55 this.selectRegion
.show(view
);
56 this.setUfGroupId(this.options
.ufGroupId
, {silent
: true});
59 onChangeUfGroupId: function(event
) {
60 this.options
.ufGroupId
= $(event
.target
).val();
61 this.trigger('change:ufGroupId', this);
65 toggleButtons: function() {
66 this.$('.crm-profile-selector-edit,.crm-profile-selector-copy').attr('disabled', !this.hasUfGroupId());
68 hasUfGroupId: function() {
69 return (this.getUfGroupId() && this.getUfGroupId() != '') ? true : false;
71 setUfGroupId: function(value
, options
) {
72 this.options
.ufGroupId
= value
;
73 this.$('.crm-profile-selector-select select').val(value
);
74 if (!options
|| !options
.silent
) {
75 this.$('.crm-profile-selector-select select').change();
78 getUfGroupId: function() {
79 return this.options
.ufGroupId
;
81 doPreview: function() {
82 var $pane
= this.$('.crm-profile-selector-preview-pane');
84 if (!this.hasUfGroupId()) {
85 $pane
.html($('#profile_selector_empty_preview_template').html());
89 $pane
.block({message
: ts('Loading...'), theme
: true});
91 url
: CRM
.url("civicrm/ajax/inline"),
94 class_name
: 'CRM_UF_Form_Inline_PreviewById',
95 id
: this.getUfGroupId(),
98 }).done(function(data
) {
103 return false; // disable buttons
109 var profileSelectorView
= this;
110 var designerDialog
= new CRM
.Designer
.DesignerDialog({
111 findCreateUfGroupModel: function(options
) {
112 var ufId
= profileSelectorView
.getUfGroupId();
113 // Retrieve UF group and fields from the api
114 CRM
.api('UFGroup', 'getsingle', {id
: ufId
, "api.UFField.get": 1}, {
115 success: function(formData
) {
116 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
117 var ufGroupModel
= new CRM
.UF
.UFGroupModel(_
.pick(formData
, _
.keys(CRM
.UF
.UFGroupModel
.prototype.schema
)));
118 ufGroupModel
.getRel('ufEntityCollection').reset(profileSelectorView
.options
.ufEntities
);
119 ufGroupModel
.getRel('ufFieldCollection').reset(_
.values(formData
["api.UFField.get"].values
));
120 options
.onLoad(ufGroupModel
);
125 CRM
.designerApp
.vent
.on('ufSaved', this.onSave
, this);
126 this.setDialog(designerDialog
);
130 // This is largely the same as doEdit, but we ultimately pass in a deepCopy of the ufGroupModel.
131 var profileSelectorView
= this;
132 var designerDialog
= new CRM
.Designer
.DesignerDialog({
133 findCreateUfGroupModel: function(options
) {
134 var ufId
= profileSelectorView
.getUfGroupId();
135 // Retrieve UF group and fields from the api
136 CRM
.api('UFGroup', 'getsingle', {id
: ufId
, "api.UFField.get": 1}, {
137 success: function(formData
) {
138 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
139 var ufGroupModel
= new CRM
.UF
.UFGroupModel(_
.pick(formData
, _
.keys(CRM
.UF
.UFGroupModel
.prototype.schema
)));
140 ufGroupModel
.getRel('ufEntityCollection').reset(profileSelectorView
.options
.ufEntities
);
141 ufGroupModel
.getRel('ufFieldCollection').reset(_
.values(formData
["api.UFField.get"].values
));
142 options
.onLoad(ufGroupModel
.deepCopy());
147 CRM
.designerApp
.vent
.on('ufSaved', this.onSave
, this);
148 this.setDialog(designerDialog
);
151 doCreate: function() {
152 var profileSelectorView
= this;
153 var designerDialog
= new CRM
.Designer
.DesignerDialog({
154 findCreateUfGroupModel: function(options
) {
155 // Initialize new UF group
156 var ufGroupModel
= new CRM
.UF
.UFGroupModel();
157 ufGroupModel
.getRel('ufEntityCollection').reset(profileSelectorView
.options
.ufEntities
);
158 options
.onLoad(ufGroupModel
);
161 CRM
.designerApp
.vent
.on('ufSaved', this.onSave
, this);
162 this.setDialog(designerDialog
);
166 CRM
.designerApp
.vent
.off('ufSaved', this.onSave
, this);
167 var ufGroupId
= this.activeDialog
.model
.get('id');
168 var modelFromCollection
= this.options
.ufGroupCollection
.get(ufGroupId
);
169 if (modelFromCollection
) {
170 // copy in changes to UFGroup
171 modelFromCollection
.set(this.activeDialog
.model
.toStrictJSON());
173 // add in new UFGroup
174 modelFromCollection
= new CRM
.UF
.UFGroupModel(this.activeDialog
.model
.toStrictJSON());
175 this.options
.ufGroupCollection
.add(modelFromCollection
);
177 this.setUfGroupId(ufGroupId
);
180 setDialog: function(view
) {
181 if (this.activeDialog
) {
182 this.activeDialog
.close();
184 this.activeDialog
= view
;