Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-21-08-12-12
[civicrm-core.git] / js / view / crm.profile-selector.js
1 (function($, _) {
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',
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'
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();
60 },
61 onChangeUfGroupId: function(event) {
62 this.options.ufGroupId = $(event.target).val();
63 this.trigger('change:ufGroupId', this);
64 this.toggleButtons();
65 this.doPreview();
66 },
67 toggleButtons: function() {
68 this.$('.crm-profile-selector-edit,.crm-profile-selector-copy').prop('disabled', !this.hasUfGroupId());
69 },
70 hasUfGroupId: function() {
71 return (this.getUfGroupId() && this.getUfGroupId() != '') ? true : false;
72 },
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();
78 }
79 },
80 getUfGroupId: function() {
81 return this.options.ufGroupId;
82 },
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());
87 } else {
88 CRM.loadPage(CRM.url("civicrm/ajax/inline", {class_name: 'CRM_UF_Form_Inline_PreviewById', id: this.getUfGroupId()}), {target: $pane});
89 }
90 },
91 disableForm: function() {
92 this.$(':input', '.crm-profile-selector-preview-pane').prop('readOnly', true);
93 },
94 doEdit: function(e) {
95 e.preventDefault();
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);
108 }
109 });
110 }
111 });
112 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
113 this.setDialog(designerDialog);
114 },
115 doCopy: function(e) {
116 e.preventDefault();
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());
130 }
131 });
132 }
133 });
134 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
135 this.setDialog(designerDialog);
136 },
137 doCreate: function(e) {
138 e.preventDefault();
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);
146 }
147 });
148 CRM.designerApp.vent.on('ufSaved', this.onSave, this);
149 this.setDialog(designerDialog);
150 },
151 onSave: function() {
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());
158 } else {
159 // add in new UFGroup
160 modelFromCollection = new CRM.UF.UFGroupModel(this.activeDialog.model.toStrictJSON());
161 this.options.ufGroupCollection.add(modelFromCollection);
162 }
163 this.setUfGroupId(ufGroupId);
164 this.doPreview();
165 },
166 setDialog: function(view) {
167 if (this.activeDialog) {
168 this.activeDialog.close();
169 }
170 this.activeDialog = view;
171 view.render();
172 }
173 });
174 })(CRM.$, CRM._);