Merge pull request #3216 from xurizaemon/CRM-14596
[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
CW
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'
6a488035
TO
50 },
51 /** @var Marionette.View which specifically builds on jQuery-UI's dialog */
52 activeDialog: null,
53 onRender: function() {
54 var view = new CRM.ProfileSelector.Select({
55 collection: this.options.ufGroupCollection
56 });
57 this.selectRegion.show(view);
58 this.setUfGroupId(this.options.ufGroupId, {silent: true});
59 this.toggleButtons();
378ea9fc 60 this.$('.crm-profile-selector-select select').css('width', '25em').crmSelect2();
6a488035
TO
61 },
62 onChangeUfGroupId: function(event) {
63 this.options.ufGroupId = $(event.target).val();
64 this.trigger('change:ufGroupId', this);
65 this.toggleButtons();
66 this.doPreview();
67 },
68 toggleButtons: function() {
6f9cd76f 69 this.$('.crm-profile-selector-edit,.crm-profile-selector-copy').prop('disabled', !this.hasUfGroupId());
6a488035
TO
70 },
71 hasUfGroupId: function() {
72 return (this.getUfGroupId() && this.getUfGroupId() != '') ? true : false;
73 },
74 setUfGroupId: function(value, options) {
75 this.options.ufGroupId = value;
858a9a31 76 this.$('.crm-profile-selector-select select').val(value);
378ea9fc 77 this.$('.crm-profile-selector-select select').select2('val', value, (!options || !options.silent));
6a488035
TO
78 },
79 getUfGroupId: function() {
80 return this.options.ufGroupId;
81 },
82 doPreview: function() {
83 var $pane = this.$('.crm-profile-selector-preview-pane');
6a488035
TO
84 if (!this.hasUfGroupId()) {
85 $pane.html($('#profile_selector_empty_preview_template').html());
fb0aac1e
CW
86 } else {
87 CRM.loadPage(CRM.url("civicrm/ajax/inline", {class_name: 'CRM_UF_Form_Inline_PreviewById', id: this.getUfGroupId()}), {target: $pane});
6a488035 88 }
6a488035 89 },
fb0aac1e
CW
90 disableForm: function() {
91 this.$(':input', '.crm-profile-selector-preview-pane').prop('readOnly', true);
92 },
93 doEdit: function(e) {
94 e.preventDefault();
6a488035
TO
95 var profileSelectorView = this;
96 var designerDialog = new CRM.Designer.DesignerDialog({
97 findCreateUfGroupModel: function(options) {
98 var ufId = profileSelectorView.getUfGroupId();
99 // Retrieve UF group and fields from the api
100 CRM.api('UFGroup', 'getsingle', {id: ufId, "api.UFField.get": 1}, {
101 success: function(formData) {
102 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
103 var ufGroupModel = new CRM.UF.UFGroupModel(_.pick(formData, _.keys(CRM.UF.UFGroupModel.prototype.schema)));
d35c2913 104 ufGroupModel.setUFGroupModel(ufGroupModel.calculateContactEntityType(), profileSelectorView.options.ufEntities);
6a488035
TO
105 ufGroupModel.getRel('ufFieldCollection').reset(_.values(formData["api.UFField.get"].values));
106 options.onLoad(ufGroupModel);
107 }
108 });
109 }
110 });
111 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
112 this.setDialog(designerDialog);
6a488035 113 },
fb0aac1e
CW
114 doCopy: function(e) {
115 e.preventDefault();
6a488035
TO
116 // This is largely the same as doEdit, but we ultimately pass in a deepCopy of the ufGroupModel.
117 var profileSelectorView = this;
118 var designerDialog = new CRM.Designer.DesignerDialog({
119 findCreateUfGroupModel: function(options) {
120 var ufId = profileSelectorView.getUfGroupId();
121 // Retrieve UF group and fields from the api
122 CRM.api('UFGroup', 'getsingle', {id: ufId, "api.UFField.get": 1}, {
123 success: function(formData) {
124 // Note: With chaining, API returns some extraneous keys that aren't part of UFGroupModel
125 var ufGroupModel = new CRM.UF.UFGroupModel(_.pick(formData, _.keys(CRM.UF.UFGroupModel.prototype.schema)));
d35c2913 126 ufGroupModel.setUFGroupModel(ufGroupModel.calculateContactEntityType(), profileSelectorView.options.ufEntities);
6a488035
TO
127 ufGroupModel.getRel('ufFieldCollection').reset(_.values(formData["api.UFField.get"].values));
128 options.onLoad(ufGroupModel.deepCopy());
129 }
130 });
131 }
132 });
133 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
134 this.setDialog(designerDialog);
6a488035 135 },
fb0aac1e
CW
136 doCreate: function(e) {
137 e.preventDefault();
6a488035
TO
138 var profileSelectorView = this;
139 var designerDialog = new CRM.Designer.DesignerDialog({
140 findCreateUfGroupModel: function(options) {
141 // Initialize new UF group
142 var ufGroupModel = new CRM.UF.UFGroupModel();
143 ufGroupModel.getRel('ufEntityCollection').reset(profileSelectorView.options.ufEntities);
144 options.onLoad(ufGroupModel);
145 }
146 });
147 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
148 this.setDialog(designerDialog);
6a488035
TO
149 },
150 onSave: function() {
151 CRM.designerApp.vent.off('ufSaved', this.onSave, this);
152 var ufGroupId = this.activeDialog.model.get('id');
153 var modelFromCollection = this.options.ufGroupCollection.get(ufGroupId);
154 if (modelFromCollection) {
155 // copy in changes to UFGroup
156 modelFromCollection.set(this.activeDialog.model.toStrictJSON());
157 } else {
158 // add in new UFGroup
159 modelFromCollection = new CRM.UF.UFGroupModel(this.activeDialog.model.toStrictJSON());
160 this.options.ufGroupCollection.add(modelFromCollection);
161 }
162 this.setUfGroupId(ufGroupId);
163 this.doPreview();
164 },
165 setDialog: function(view) {
166 if (this.activeDialog) {
167 this.activeDialog.close();
168 }
169 this.activeDialog = view;
170 view.render();
171 }
172 });
4b513f23 173})(CRM.$, CRM._);