Merge pull request #4831 from williamtheaker/master
[civicrm-core.git] / js / view / crm.profile-selector.js
CommitLineData
4b513f23 1(function($, _) {
6a488035
TO
2 if (!CRM.ProfileSelector) CRM.ProfileSelector = {};
3
4 CRM.ProfileSelector.Option = Backbone.Marionette.ItemView.extend({
5 template: '#profile_selector_option_template',
6 tagName: 'option',
7 modelEvents: {
8 'change:title': 'render'
9 },
10 onRender: function() {
11 this.$el.attr('value', this.model.get('id'));
12 }
13 });
14
15 CRM.ProfileSelector.Select = Backbone.Marionette.CollectionView.extend({
16 tagName: 'select',
17 itemView: CRM.ProfileSelector.Option
18 });
19
20 /**
21 * Render a pane with 'Select/Preview/Edit/Copy/Create' functionality for profiles.
22 *
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
28 * on the server.
29 *
30 * options:
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)
36 */
37 CRM.ProfileSelector.View = Backbone.Marionette.Layout.extend({
38 template: '#profile_selector_template',
39 regions: {
40 selectRegion: '.crm-profile-selector-select'
41 },
42 events: {
43 'change .crm-profile-selector-select select': 'onChangeUfGroupId',
44 'click .crm-profile-selector-edit': 'doEdit',
45 'click .crm-profile-selector-copy': 'doCopy',
fb0aac1e 46 'click .crm-profile-selector-create': 'doCreate',
413923b2 47 'click .crm-profile-selector-preview': 'doShowPreview',
fb0aac1e
CW
48 // prevent interaction with preview form
49 'click .crm-profile-selector-preview-pane': false,
50 'crmLoad .crm-profile-selector-preview-pane': 'disableForm'
6a488035
TO
51 },
52 /** @var Marionette.View which specifically builds on jQuery-UI's dialog */
53 activeDialog: null,
54 onRender: function() {
55 var view = new CRM.ProfileSelector.Select({
56 collection: this.options.ufGroupCollection
57 });
58 this.selectRegion.show(view);
59 this.setUfGroupId(this.options.ufGroupId, {silent: true});
60 this.toggleButtons();
378ea9fc 61 this.$('.crm-profile-selector-select select').css('width', '25em').crmSelect2();
413923b2 62 this.doShowPreview();
6a488035
TO
63 },
64 onChangeUfGroupId: function(event) {
65 this.options.ufGroupId = $(event.target).val();
66 this.trigger('change:ufGroupId', this);
67 this.toggleButtons();
68 this.doPreview();
69 },
70 toggleButtons: function() {
6f9cd76f 71 this.$('.crm-profile-selector-edit,.crm-profile-selector-copy').prop('disabled', !this.hasUfGroupId());
6a488035
TO
72 },
73 hasUfGroupId: function() {
d9e49ea2 74 return (this.getUfGroupId() && this.getUfGroupId() !== '') ? true : false;
6a488035
TO
75 },
76 setUfGroupId: function(value, options) {
77 this.options.ufGroupId = value;
858a9a31 78 this.$('.crm-profile-selector-select select').val(value);
378ea9fc 79 this.$('.crm-profile-selector-select select').select2('val', value, (!options || !options.silent));
6a488035
TO
80 },
81 getUfGroupId: function() {
82 return this.options.ufGroupId;
83 },
84 doPreview: function() {
85 var $pane = this.$('.crm-profile-selector-preview-pane');
6a488035
TO
86 if (!this.hasUfGroupId()) {
87 $pane.html($('#profile_selector_empty_preview_template').html());
fb0aac1e
CW
88 } else {
89 CRM.loadPage(CRM.url("civicrm/ajax/inline", {class_name: 'CRM_UF_Form_Inline_PreviewById', id: this.getUfGroupId()}), {target: $pane});
6a488035 90 }
6a488035 91 },
413923b2
DG
92 doShowPreview: function() {
93 var $preview = this.$('.crm-profile-selector-preview');
94 var $pane = this.$('.crm-profile-selector-preview-pane');
95 if ($preview.hasClass('crm-profile-selector-preview-show')) {
96 $preview.removeClass('crm-profile-selector-preview-show');
97 $pane.show();
98 } else {
99 $preview.addClass('crm-profile-selector-preview-show');
100 $pane.hide();
101 }
102 },
fb0aac1e 103 disableForm: function() {
852cbcc6 104 this.$(':input', '.crm-profile-selector-preview-pane').not('.select2-input').prop('readOnly', true);
fb0aac1e
CW
105 },
106 doEdit: function(e) {
107 e.preventDefault();
6a488035
TO
108 var profileSelectorView = this;
109 var designerDialog = new CRM.Designer.DesignerDialog({
110 findCreateUfGroupModel: function(options) {
111 var ufId = profileSelectorView.getUfGroupId();
112 // Retrieve UF group and fields from the api
113 CRM.api('UFGroup', 'getsingle', {id: ufId, "api.UFField.get": 1}, {
114 success: function(formData) {
115 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
116 var ufGroupModel = new CRM.UF.UFGroupModel(_.pick(formData, _.keys(CRM.UF.UFGroupModel.prototype.schema)));
d35c2913 117 ufGroupModel.setUFGroupModel(ufGroupModel.calculateContactEntityType(), profileSelectorView.options.ufEntities);
6a488035
TO
118 ufGroupModel.getRel('ufFieldCollection').reset(_.values(formData["api.UFField.get"].values));
119 options.onLoad(ufGroupModel);
120 }
121 });
122 }
123 });
124 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
125 this.setDialog(designerDialog);
6a488035 126 },
fb0aac1e
CW
127 doCopy: function(e) {
128 e.preventDefault();
6a488035
TO
129 // This is largely the same as doEdit, but we ultimately pass in a deepCopy of the ufGroupModel.
130 var profileSelectorView = this;
131 var designerDialog = new CRM.Designer.DesignerDialog({
132 findCreateUfGroupModel: function(options) {
133 var ufId = profileSelectorView.getUfGroupId();
134 // Retrieve UF group and fields from the api
135 CRM.api('UFGroup', 'getsingle', {id: ufId, "api.UFField.get": 1}, {
136 success: function(formData) {
137 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
138 var ufGroupModel = new CRM.UF.UFGroupModel(_.pick(formData, _.keys(CRM.UF.UFGroupModel.prototype.schema)));
d35c2913 139 ufGroupModel.setUFGroupModel(ufGroupModel.calculateContactEntityType(), profileSelectorView.options.ufEntities);
6a488035
TO
140 ufGroupModel.getRel('ufFieldCollection').reset(_.values(formData["api.UFField.get"].values));
141 options.onLoad(ufGroupModel.deepCopy());
142 }
143 });
144 }
145 });
146 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
147 this.setDialog(designerDialog);
6a488035 148 },
fb0aac1e
CW
149 doCreate: function(e) {
150 e.preventDefault();
6a488035
TO
151 var profileSelectorView = this;
152 var designerDialog = new CRM.Designer.DesignerDialog({
153 findCreateUfGroupModel: function(options) {
154 // Initialize new UF group
155 var ufGroupModel = new CRM.UF.UFGroupModel();
156 ufGroupModel.getRel('ufEntityCollection').reset(profileSelectorView.options.ufEntities);
157 options.onLoad(ufGroupModel);
158 }
159 });
160 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
161 this.setDialog(designerDialog);
6a488035
TO
162 },
163 onSave: function() {
164 CRM.designerApp.vent.off('ufSaved', this.onSave, this);
165 var ufGroupId = this.activeDialog.model.get('id');
166 var modelFromCollection = this.options.ufGroupCollection.get(ufGroupId);
167 if (modelFromCollection) {
168 // copy in changes to UFGroup
169 modelFromCollection.set(this.activeDialog.model.toStrictJSON());
170 } else {
171 // add in new UFGroup
172 modelFromCollection = new CRM.UF.UFGroupModel(this.activeDialog.model.toStrictJSON());
173 this.options.ufGroupCollection.add(modelFromCollection);
174 }
175 this.setUfGroupId(ufGroupId);
176 this.doPreview();
177 },
178 setDialog: function(view) {
179 if (this.activeDialog) {
180 this.activeDialog.close();
181 }
182 this.activeDialog = view;
183 view.render();
184 }
185 });
4b513f23 186})(CRM.$, CRM._);