Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | (function($) { |
2 | if (!CRM.Designer) CRM.Designer = {}; | |
3 | ||
4 | /** | |
5 | * When rendering a template with Marionette.ItemView, the list of variables is determined by | |
6 | * serializeData(). The normal behavior is to map each property of this.model to a template | |
7 | * variable. | |
8 | * | |
9 | * This function extends that practice by exporting variables "_view", "_model", "_collection", | |
10 | * and "_options". This makes it easier for the template to, e.g., access computed properties of | |
11 | * a model (by calling "_model.getComputedProperty"), or to access constructor options (by | |
12 | * calling "_options.myoption"). | |
13 | * | |
14 | * @return {*} | |
15 | */ | |
16 | var extendedSerializeData = function() { | |
17 | var result = Marionette.ItemView.prototype.serializeData.apply(this); | |
18 | result._view = this; | |
19 | result._model = this.model; | |
20 | result._collection = this.collection; | |
21 | result._options = this.options; | |
22 | return result; | |
abc8b55b | 23 | }; |
6a488035 TO |
24 | |
25 | /** | |
26 | * Display a dialog window with an editable form for a UFGroupModel | |
27 | * | |
28 | * The implementation here is very "jQuery-style" and not "Backbone-style"; | |
29 | * it's been extracted | |
30 | * | |
31 | * options: | |
32 | * - model: CRM.UF.UFGroupModel | |
33 | */ | |
34 | CRM.Designer.DesignerDialog = Backbone.Marionette.Layout.extend({ | |
35 | serializeData: extendedSerializeData, | |
36 | template: '#designer_dialog_template', | |
37 | className: 'crm-designer-dialog', | |
38 | regions: { | |
39 | designerRegion: '.crm-designer' | |
40 | }, | |
41 | /** @var bool whether this dialog is currently open */ | |
42 | isDialogOpen: false, | |
43 | /** @var bool whether any changes have been made */ | |
44 | isUfUnsaved: false, | |
45 | /** @var obj handle for the CRM.alert containing undo link */ | |
46 | undoAlert: null, | |
47 | /** @var bool whether this dialog is being re-opened by the undo link */ | |
48 | undoState: false, | |
49 | ||
50 | initialize: function(options) { | |
51 | CRM.designerApp.vent.on('ufUnsaved', this.onUfChanged, this); | |
52 | }, | |
53 | onClose: function() { | |
54 | this.undoAlert && this.undoAlert.close && this.undoAlert.close(); | |
55 | CRM.designerApp.vent.off('ufUnsaved', this.onUfChanged, this); | |
56 | }, | |
57 | onUfChanged: function(isUfUnsaved) { | |
58 | this.isUfUnsaved = isUfUnsaved; | |
59 | }, | |
60 | onRender: function() { | |
61 | var designerDialog = this; | |
62 | designerDialog.$el.dialog({ | |
63 | autoOpen: true, // note: affects accordion height | |
64 | title: 'Edit Profile', | |
65 | width: '75%', | |
66 | height: 600, | |
67 | minWidth: 500, | |
68 | minHeight: 600, // to allow dropping in big whitespace, coordinate with min-height of .crm-designer-fields | |
69 | open: function() { | |
70 | // Prevent conflicts with other onbeforeunload handlers | |
71 | designerDialog.oldOnBeforeUnload = window.onbeforeunload; | |
72 | // Warn of unsaved changes when navigating away from the page | |
73 | window.onbeforeunload = function() { | |
74 | if (designerDialog.isDialogOpen && designerDialog.isUfUnsaved) { | |
75 | return ts("Your profile has not been saved."); | |
76 | } | |
77 | if (designerDialog.oldOnBeforeUnload) { | |
78 | return designerDialog.oldOnBeforeUnload.apply(arguments); | |
79 | } | |
80 | }; | |
81 | designerDialog.undoAlert && designerDialog.undoAlert.close && designerDialog.undoAlert.close(); | |
82 | designerDialog.isDialogOpen = true; | |
83 | // Initialize new dialog if we are not re-opening unsaved changes | |
84 | if (designerDialog.undoState === false) { | |
85 | designerDialog.designerRegion && designerDialog.designerRegion.close && designerDialog.designerRegion.close(); | |
86 | designerDialog.$el.block({message: 'Loading...', theme: true}); | |
87 | designerDialog.options.findCreateUfGroupModel({ | |
88 | onLoad: function(ufGroupModel) { | |
89 | designerDialog.model = ufGroupModel; | |
90 | var designerLayout = new CRM.Designer.DesignerLayout({ | |
91 | model: ufGroupModel, | |
92 | el: '<div class="full-height"></div>' | |
93 | }); | |
94 | designerDialog.$el.unblock(); | |
95 | designerDialog.designerRegion.show(designerLayout); | |
96 | CRM.designerApp.vent.trigger('resize'); | |
97 | designerDialog.isUfUnsaved = false; | |
98 | } | |
99 | }); | |
100 | } | |
101 | designerDialog.undoState = false; | |
4151fff2 CW |
102 | // CRM-12188 |
103 | CRM.designerApp.DetachedProfiles = []; | |
6a488035 TO |
104 | }, |
105 | close: function() { | |
106 | window.onbeforeunload = designerDialog.oldOnBeforeUnload; | |
107 | designerDialog.isDialogOpen = false; | |
108 | ||
109 | designerDialog.undoAlert && designerDialog.undoAlert.close && designerDialog.undoAlert.close(); | |
110 | if (designerDialog.isUfUnsaved) { | |
111 | designerDialog.undoAlert = CRM.alert('<p>' + ts('Your changes to "%1" have not been saved.', {1: designerDialog.model.get('title')}) + '</p><a href="#" class="crm-undo">' + ts('Restore unsaved changes') + '</a>', ts('Unsaved Changes'), 'alert', {expires: 60000}); | |
112 | $('.ui-notify-message a.crm-undo').click(function() { | |
113 | designerDialog.undoState = true; | |
114 | designerDialog.$el.dialog('open'); | |
115 | return false; | |
116 | }); | |
117 | } | |
4151fff2 CW |
118 | // CRM-12188 |
119 | CRM.designerApp.restorePreviewArea(); | |
6a488035 TO |
120 | }, |
121 | resize: function() { | |
122 | CRM.designerApp.vent.trigger('resize'); | |
123 | } | |
124 | }); | |
125 | } | |
126 | }); | |
127 | ||
128 | /** | |
129 | * Display a complete form-editing UI, including canvas, palette, and | |
130 | * buttons. | |
131 | * | |
132 | * options: | |
133 | * - model: CRM.UF.UFGroupModel | |
134 | */ | |
135 | CRM.Designer.DesignerLayout = Backbone.Marionette.Layout.extend({ | |
136 | serializeData: extendedSerializeData, | |
137 | template: '#designer_template', | |
138 | regions: { | |
139 | buttons: '.crm-designer-buttonset-region', | |
140 | palette: '.crm-designer-palette-region', | |
141 | form: '.crm-designer-form-region', | |
142 | fields: '.crm-designer-fields-region' | |
143 | }, | |
144 | initialize: function() { | |
145 | CRM.designerApp.vent.on('resize', this.onResize, this); | |
146 | }, | |
147 | onClose: function() { | |
148 | CRM.designerApp.vent.off('resize', this.onResize, this); | |
149 | }, | |
150 | onRender: function() { | |
151 | this.buttons.show(new CRM.Designer.ToolbarView({ | |
152 | model: this.model | |
153 | })); | |
154 | this.palette.show(new CRM.Designer.PaletteView({ | |
155 | model: this.model | |
156 | })); | |
157 | this.form.show(new CRM.Designer.UFGroupView({ | |
158 | model: this.model | |
159 | })); | |
160 | this.fields.show(new CRM.Designer.UFFieldCanvasView({ | |
161 | model: this.model | |
162 | })); | |
163 | }, | |
164 | onResize: function() { | |
165 | if (! this.hasResizedBefore) { | |
166 | this.hasResizedBefore = true; | |
167 | this.$('.crm-designer-toolbar').resizable({ | |
168 | handles: 'w', | |
169 | maxWidth: 400, | |
170 | minWidth: 150, | |
171 | resize: function(event, ui) { | |
172 | $('.crm-designer-canvas').css('margin-right', (ui.size.width + 10) + 'px'); | |
173 | $(this).css({left: '', height: ''}); | |
174 | } | |
175 | }).css({left: '', height: ''}); | |
176 | } | |
177 | } | |
178 | }); | |
179 | ||
180 | /** | |
181 | * Display toolbar with working button | |
182 | * | |
183 | * options: | |
184 | * - model: CRM.UF.UFGroupModel | |
185 | */ | |
186 | CRM.Designer.ToolbarView = Backbone.Marionette.ItemView.extend({ | |
187 | serializeData: extendedSerializeData, | |
188 | template: '#designer_buttons_template', | |
189 | previewMode: false, | |
190 | events: { | |
191 | 'click .crm-designer-save': 'doSave', | |
192 | 'click .crm-designer-preview': 'doPreview' | |
193 | }, | |
194 | onRender: function() { | |
95269eab CW |
195 | this.$('.crm-designer-save').button().attr({ |
196 | disabled: 'disabled', | |
197 | style: 'opacity:.5; box-shadow:none; cursor:default;' | |
198 | }); | |
6a488035 TO |
199 | this.$('.crm-designer-preview').button(); |
200 | }, | |
95269eab CW |
201 | initialize: function(options) { |
202 | CRM.designerApp.vent.on('ufUnsaved', this.onUfChanged, this); | |
203 | }, | |
204 | onUfChanged: function(isUfUnsaved) { | |
205 | if (isUfUnsaved) { | |
6f9cd76f | 206 | this.$('.crm-designer-save').removeAttr('style').prop('disabled', false); |
95269eab CW |
207 | } |
208 | }, | |
6a488035 TO |
209 | doSave: function(event) { |
210 | var ufGroupModel = this.model; | |
211 | if (ufGroupModel.getRel('ufFieldCollection').hasDuplicates()) { | |
212 | CRM.alert(ts('Please correct errors before saving.'), '', 'alert'); | |
213 | return; | |
214 | } | |
215 | var $dialog = this.$el.closest('.crm-designer-dialog'); // FIXME use events | |
216 | $dialog.block({message: 'Saving...', theme: true}); | |
217 | var profile = ufGroupModel.toStrictJSON(); | |
218 | profile["api.UFField.replace"] = {values: ufGroupModel.getRel('ufFieldCollection').toSortedJSON(), 'option.autoweight': 0}; | |
219 | CRM.api('UFGroup', 'create', profile, { | |
220 | success: function(data) { | |
221 | $dialog.unblock(); | |
222 | var error = false; | |
223 | if (data.is_error) { | |
224 | CRM.alert(data.error_message); | |
225 | error = true; | |
226 | } | |
227 | _.each(data.values, function(ufGroupResponse) { | |
228 | if (ufGroupResponse['api.UFField.replace'].is_error) { | |
229 | CRM.alert(ufGroupResponse['api.UFField.replace'].error_message); | |
230 | error = true; | |
231 | } | |
232 | }); | |
233 | if (!error) { | |
234 | if (!ufGroupModel.get('id')) { | |
235 | ufGroupModel.set('id', data.id); | |
236 | } | |
237 | CRM.designerApp.vent.trigger('ufUnsaved', false); | |
238 | CRM.designerApp.vent.trigger('ufSaved'); | |
239 | $dialog.dialog('close'); | |
240 | } | |
241 | } | |
242 | }); | |
243 | return false; | |
244 | }, | |
245 | doPreview: function(event) { | |
246 | this.previewMode = !this.previewMode; | |
247 | if (!this.previewMode) { | |
248 | $('.crm-designer-preview-canvas').html(''); | |
249 | $('.crm-designer-canvas > *, .crm-designer-palette-region').show(); | |
250 | $('.crm-designer-preview span').html(ts('Preview')); | |
251 | return; | |
252 | } | |
253 | if (this.model.getRel('ufFieldCollection').hasDuplicates()) { | |
254 | CRM.alert(ts('Please correct errors before previewing.'), '', 'alert'); | |
255 | return; | |
256 | } | |
257 | var $dialog = this.$el.closest('.crm-designer-dialog'); // FIXME use events | |
258 | $dialog.block({message: 'Loading...', theme: true}); | |
4151fff2 CW |
259 | // CRM-12188 |
260 | CRM.designerApp.clearPreviewArea(); | |
6a488035 TO |
261 | $.ajax({ |
262 | url: CRM.url("civicrm/ajax/inline"), | |
263 | type: 'POST', | |
264 | data: { | |
265 | 'qfKey': CRM.profilePreviewKey, | |
266 | 'class_name': 'CRM_UF_Form_Inline_Preview', | |
267 | 'snippet': 1, | |
268 | 'ufData': JSON.stringify({ | |
269 | ufGroup: this.model.toStrictJSON(), | |
270 | ufFieldCollection: this.model.getRel('ufFieldCollection').toSortedJSON() | |
271 | }) | |
272 | } | |
273 | }).done(function(data) { | |
274 | $dialog.unblock(); | |
275 | $('.crm-designer-canvas > *, .crm-designer-palette-region').hide(); | |
276 | $('.crm-designer-preview-canvas').html(data).show(); | |
277 | $('.crm-designer-preview span').html(ts('Edit')); | |
278 | }); | |
279 | return false; | |
280 | } | |
281 | }); | |
282 | ||
283 | /** | |
284 | * Display a selection of available fields | |
285 | * | |
286 | * options: | |
287 | * - model: CRM.UF.UFGroupModel | |
288 | */ | |
289 | CRM.Designer.PaletteView = Backbone.Marionette.ItemView.extend({ | |
290 | serializeData: extendedSerializeData, | |
291 | template: '#palette_template', | |
292 | el: '<div class="full-height"></div>', | |
e871d6eb | 293 | openTreeNodes: [], |
6a488035 TO |
294 | events: { |
295 | 'keyup .crm-designer-palette-search input': 'doSearch', | |
296 | 'click .crm-designer-palette-clear-search': 'clearSearch', | |
e871d6eb CW |
297 | 'click .crm-designer-palette-toggle': 'toggleAll', |
298 | 'click .crm-designer-palette-add button': 'doNewCustomFieldDialog', | |
2b83aa0d | 299 | 'click #crm-designer-add-custom-set': 'doNewCustomSetDialog', |
e871d6eb | 300 | 'dblclick .crm-designer-palette-field': 'doAddToCanvas' |
6a488035 TO |
301 | }, |
302 | initialize: function() { | |
303 | this.model.getRel('ufFieldCollection') | |
304 | .on('add', this.toggleActive, this) | |
305 | .on('remove', this.toggleActive, this); | |
306 | this.model.getRel('paletteFieldCollection') | |
307 | .on('reset', this.render, this); | |
308 | CRM.designerApp.vent.on('resize', this.onResize, this); | |
309 | }, | |
310 | onClose: function() { | |
311 | this.model.getRel('ufFieldCollection') | |
312 | .off('add', this.toggleActive, this) | |
313 | .off('remove', this.toggleActive, this); | |
314 | this.model.getRel('paletteFieldCollection') | |
315 | .off('reset', this.render, this); | |
316 | CRM.designerApp.vent.off('resize', this.onResize, this); | |
317 | }, | |
318 | onRender: function() { | |
319 | var paletteView = this; | |
320 | ||
321 | // Prepare data for jstree | |
322 | var treeData = []; | |
323 | var paletteFieldsByEntitySection = this.model.getRel('paletteFieldCollection').getFieldsByEntitySection(); | |
324 | ||
325 | paletteView.model.getRel('ufEntityCollection').each(function(ufEntityModel){ | |
326 | _.each(ufEntityModel.getSections(), function(section, sectionKey){ | |
1d36b248 | 327 | var entitySection = ufEntityModel.get('entity_name') + '-' + sectionKey; |
6a488035 | 328 | var items = []; |
1d36b248 | 329 | if (paletteFieldsByEntitySection[entitySection]) { |
330 | _.each(paletteFieldsByEntitySection[entitySection], function(paletteFieldModel, k) { | |
331 | items.push({data: paletteFieldModel.getLabel(), attr: {'class': 'crm-designer-palette-field', 'data-plm-cid': paletteFieldModel.cid}}); | |
332 | }); | |
333 | } | |
334 | if (section.is_addable) { | |
335 | items.push({data: ts('+ Add New Field'), attr: {'class': 'crm-designer-palette-add'}}); | |
336 | } | |
337 | if (items.length > 0) { | |
338 | treeData.push({ | |
339 | data: section.title, | |
340 | children: items, | |
341 | state: _.contains(paletteView.openTreeNodes, sectionKey) ? 'open' : 'closed', | |
342 | attr: { | |
343 | 'class': 'crm-designer-palette-section', | |
344 | 'data-section': sectionKey, | |
345 | 'data-entity': ufEntityModel.get('entity_name') | |
346 | } | |
347 | }); | |
348 | } | |
6a488035 TO |
349 | }) |
350 | }); | |
351 | ||
352 | this.$('.crm-designer-palette-tree').jstree({ | |
353 | 'json_data': {data: treeData}, | |
354 | 'search': { | |
355 | 'case_insensitive' : true, | |
356 | 'show_only_matches': true | |
357 | }, | |
358 | themes: { | |
359 | "theme": 'classic', | |
360 | "dots": false, | |
4441df69 CW |
361 | "icons": false, |
362 | "url": CRM.config.resourceBase + 'packages/jquery/plugins/jstree/themes/classic/style.css' | |
6a488035 TO |
363 | }, |
364 | 'plugins': ['themes', 'json_data', 'ui', 'search'] | |
365 | }).bind('loaded.jstree', function () { | |
366 | $('.crm-designer-palette-field', this).draggable({ | |
367 | appendTo: '.crm-designer', | |
368 | zIndex: $(this.$el).zIndex() + 5000, | |
369 | helper: 'clone', | |
370 | connectToSortable: '.crm-designer-fields' // FIXME: tight canvas/palette coupling | |
371 | }); | |
6a488035 TO |
372 | paletteView.model.getRel('ufFieldCollection').each(function(ufFieldModel) { |
373 | paletteView.toggleActive(ufFieldModel, paletteView.model.getRel('ufFieldCollection')) | |
374 | }); | |
e871d6eb | 375 | paletteView.$('.crm-designer-palette-add a').replaceWith('<button>' + $('.crm-designer-palette-add a').first().text() + '</<button>'); |
2b83aa0d CW |
376 | paletteView.$('.crm-designer-palette-tree > ul').append('<li><button id="crm-designer-add-custom-set">+ ' + ts('Add Set of Custom Fields') + '</button></li>'); |
377 | paletteView.$('.crm-designer-palette-tree button').button(); | |
6a488035 TO |
378 | }).bind("select_node.jstree", function (e, data) { |
379 | $(this).jstree("toggle_node", data.rslt.obj); | |
380 | $(this).jstree("deselect_node", data.rslt.obj); | |
381 | }); | |
382 | ||
383 | // FIXME: tight canvas/palette coupling | |
384 | this.$(".crm-designer-fields").droppable({ | |
385 | activeClass: "ui-state-default", | |
386 | hoverClass: "ui-state-hover", | |
387 | accept: ":not(.ui-sortable-helper)" | |
388 | }); | |
389 | ||
390 | this.onResize(); | |
391 | }, | |
392 | onResize: function() { | |
393 | var pos = this.$('.crm-designer-palette-tree').position(); | |
394 | var div = this.$('.crm-designer-palette-tree').closest('.crm-container').height(); | |
395 | this.$('.crm-designer-palette-tree').css({height: div - pos.top}); | |
396 | }, | |
397 | doSearch: function(event) { | |
398 | $('.crm-designer-palette-tree').jstree("search", $(event.target).val()); | |
399 | }, | |
e871d6eb CW |
400 | doAddToCanvas: function(event) { |
401 | var paletteFieldModel = this.model.getRel('paletteFieldCollection').get($(event.currentTarget).attr('data-plm-cid')); | |
402 | paletteFieldModel.addToUFCollection(this.model.getRel('ufFieldCollection')); | |
403 | event.stopPropagation(); | |
404 | }, | |
405 | doNewCustomFieldDialog: function(event) { | |
6a488035 | 406 | var paletteView = this; |
e871d6eb CW |
407 | var entityKey = $(event.currentTarget).closest('.crm-designer-palette-section').attr('data-entity'); |
408 | var sectionKey = $(event.currentTarget).closest('.crm-designer-palette-section').attr('data-section'); | |
409 | var ufEntityModel = paletteView.model.getRel('ufEntityCollection').getByName(entityKey); | |
410 | var sections = ufEntityModel.getSections(); | |
3fde8ee5 CW |
411 | var url = CRM.url('civicrm/admin/custom/group/field/add', { |
412 | reset: 1, | |
413 | action: 'add', | |
e871d6eb | 414 | gid: sections[sectionKey].custom_group_id |
3fde8ee5 | 415 | }); |
205bb8ae CW |
416 | CRM.loadForm(url).on('crmFormSuccess', function(e, data) { |
417 | paletteView.doRefresh('custom_' + data.id); | |
3fde8ee5 | 418 | }); |
6a488035 TO |
419 | return false; |
420 | }, | |
2b83aa0d CW |
421 | doNewCustomSetDialog: function(event) { |
422 | var paletteView = this; | |
423 | var url = CRM.url('civicrm/admin/custom/group', 'action=add&reset=1'); | |
424 | // Create custom field set and automatically go to next step (create fields) after save button is clicked. | |
425 | CRM.loadForm(url, {refreshAction: ['next']}) | |
426 | .on('crmFormSuccess', function(e, data) { | |
427 | // When form switches to create custom field context, modify button behavior to only continue for "save and new" | |
428 | data.customField && ($(this).data('crmSnippet').options.crmForm.refreshAction = ['next_new']); | |
429 | paletteView.doRefresh(data.customField ? 'custom_' + data.id : null); | |
430 | }); | |
431 | return false; | |
432 | }, | |
0b1bae4a | 433 | doRefresh: function(fieldToAdd) { |
6a488035 | 434 | var ufGroupModel = this.model; |
e871d6eb | 435 | this.getOpenTreeNodes(); |
6a488035 TO |
436 | CRM.Schema.reloadModels() |
437 | .done(function(data){ | |
438 | ufGroupModel.resetEntities(); | |
0b1bae4a CW |
439 | if (fieldToAdd) { |
440 | var field = ufGroupModel.getRel('paletteFieldCollection').getFieldByName(null, fieldToAdd); | |
441 | field.addToUFCollection(ufGroupModel.getRel('ufFieldCollection')); | |
442 | } | |
6a488035 TO |
443 | }) |
444 | .fail(function() { | |
445 | CRM.alert(ts('Failed to retrieve schema'), ts('Error'), 'error'); | |
446 | }); | |
6a488035 TO |
447 | }, |
448 | clearSearch: function(event) { | |
449 | $('.crm-designer-palette-search input').val('').keyup(); | |
450 | return false; | |
451 | }, | |
452 | toggleActive: function(ufFieldModel, ufFieldCollection, options) { | |
453 | var paletteFieldCollection = this.model.getRel('paletteFieldCollection'); | |
454 | var paletteFieldModel = paletteFieldCollection.getFieldByName(ufFieldModel.get('entity_name'), ufFieldModel.get('field_name')); | |
455 | var isAddable = ufFieldCollection.isAddable(ufFieldModel); | |
456 | this.$('[data-plm-cid='+paletteFieldModel.cid+']').toggleClass('disabled', !isAddable); | |
457 | }, | |
458 | toggleAll: function(event) { | |
459 | if ($('.crm-designer-palette-search input').val() == '') { | |
460 | $('.crm-designer-palette-tree').jstree($(event.target).attr('rel')); | |
461 | } | |
462 | return false; | |
e871d6eb CW |
463 | }, |
464 | getOpenTreeNodes: function() { | |
465 | var paletteView = this; | |
466 | this.openTreeNodes = []; | |
467 | this.$('.crm-designer-palette-section.jstree-open').each(function() { | |
468 | paletteView.openTreeNodes.push($(this).data('section')); | |
469 | }) | |
6a488035 TO |
470 | } |
471 | }); | |
472 | ||
473 | /** | |
474 | * Display all UFFieldModel objects in a UFGroupModel. | |
475 | * | |
476 | * options: | |
477 | * - model: CRM.UF.UFGroupModel | |
478 | */ | |
479 | CRM.Designer.UFFieldCanvasView = Backbone.Marionette.View.extend({ | |
480 | initialize: function() { | |
481 | this.model.getRel('ufFieldCollection') | |
482 | .on('add', this.updatePlaceholder, this) | |
483 | .on('remove', this.updatePlaceholder, this) | |
484 | .on('add', this.addUFFieldView, this); | |
485 | }, | |
486 | onClose: function() { | |
487 | this.model.getRel('ufFieldCollection') | |
488 | .off('add', this.updatePlaceholder, this) | |
489 | .off('remove', this.updatePlaceholder, this) | |
490 | .off('add', this.addUFFieldView, this); | |
491 | }, | |
492 | render: function() { | |
493 | var ufFieldCanvasView = this; | |
494 | this.$el.html(_.template($('#field_canvas_view_template').html())); | |
495 | ||
496 | // BOTTOM: Setup field-level editing | |
497 | var $fields = this.$('.crm-designer-fields'); | |
498 | this.updatePlaceholder(); | |
499 | var ufFieldModels = this.model.getRel('ufFieldCollection').sortBy(function(ufFieldModel) { | |
500 | return parseInt(ufFieldModel.get('weight')); | |
501 | }); | |
502 | _.each(ufFieldModels, function(ufFieldModel) { | |
503 | ufFieldCanvasView.addUFFieldView(ufFieldModel, ufFieldCanvasView.model.getRel('ufFieldCollection'), {skipWeights: true}); | |
504 | }); | |
505 | this.$(".crm-designer-fields").sortable({ | |
506 | placeholder: 'crm-designer-row-placeholder', | |
507 | forcePlaceholderSize: true, | |
508 | receive: function(event, ui) { | |
509 | var paletteFieldModel = ufFieldCanvasView.model.getRel('paletteFieldCollection').get(ui.item.attr('data-plm-cid')); | |
510 | var ufFieldModel = paletteFieldModel.addToUFCollection( | |
511 | ufFieldCanvasView.model.getRel('ufFieldCollection'), | |
512 | {skipWeights: true} | |
513 | ); | |
514 | if (null == ufFieldModel) { | |
515 | ufFieldCanvasView.$('.crm-designer-fields .ui-draggable').remove(); | |
516 | } else { | |
517 | // Move from end to the 'dropped' position | |
518 | var ufFieldViewEl = ufFieldCanvasView.$('div[data-field-cid='+ufFieldModel.cid+']').parent(); | |
519 | ufFieldCanvasView.$('.crm-designer-fields .ui-draggable').replaceWith(ufFieldViewEl); | |
520 | } | |
521 | // note: the sortable() update callback will call updateWeight | |
522 | }, | |
523 | update: function() { | |
524 | ufFieldCanvasView.updateWeights(); | |
525 | } | |
526 | }); | |
527 | }, | |
528 | /** Determine visual order of fields and set the model values for "weight" */ | |
529 | updateWeights: function() { | |
530 | var ufFieldCanvasView = this; | |
531 | var weight = 1; | |
532 | var rows = this.$('.crm-designer-row').each(function(key, row) { | |
533 | if ($(row).hasClass('placeholder')) { | |
534 | return; | |
535 | } | |
536 | var ufFieldCid = $(row).attr('data-field-cid'); | |
537 | var ufFieldModel = ufFieldCanvasView.model.getRel('ufFieldCollection').get(ufFieldCid); | |
538 | ufFieldModel.set('weight', weight); | |
539 | weight++; | |
540 | }); | |
541 | }, | |
542 | addUFFieldView: function(ufFieldModel, ufFieldCollection, options) { | |
543 | var paletteFieldModel = this.model.getRel('paletteFieldCollection').getFieldByName(ufFieldModel.get('entity_name'), ufFieldModel.get('field_name')); | |
544 | var ufFieldView = new CRM.Designer.UFFieldView({ | |
545 | el: $("<div></div>"), | |
546 | model: ufFieldModel, | |
547 | paletteFieldModel: paletteFieldModel | |
548 | }); | |
549 | ufFieldView.render(); | |
550 | this.$('.crm-designer-fields').append(ufFieldView.$el); | |
551 | if (! (options && options.skipWeights)) { | |
552 | this.updateWeights(); | |
553 | } | |
554 | }, | |
555 | updatePlaceholder: function() { | |
556 | if (this.model.getRel('ufFieldCollection').isEmpty()) { | |
557 | this.$('.placeholder').css({display: 'block', border: '0 none', cursor: 'default'}); | |
558 | } else { | |
559 | this.$('.placeholder').hide(); | |
560 | } | |
561 | } | |
562 | }); | |
563 | ||
564 | /** | |
565 | * options: | |
566 | * - model: CRM.UF.UFFieldModel | |
567 | * - paletteFieldModel: CRM.Designer.PaletteFieldModel | |
568 | */ | |
569 | CRM.Designer.UFFieldView = Backbone.Marionette.Layout.extend({ | |
570 | serializeData: extendedSerializeData, | |
571 | template: '#field_row_template', | |
572 | expanded: false, | |
573 | regions: { | |
574 | summary: '.crm-designer-field-summary', | |
575 | detail: '.crm-designer-field-detail' | |
576 | }, | |
577 | events: { | |
578 | "click .crm-designer-action-settings": 'doToggleForm', | |
2b83aa0d | 579 | "click button.crm-designer-edit-custom": 'doEditCustomField', |
6a488035 TO |
580 | "click .crm-designer-action-remove": 'doRemove' |
581 | }, | |
582 | modelEvents: { | |
583 | "destroy": 'remove', | |
584 | "change:is_duplicate": 'onChangeIsDuplicate' | |
585 | }, | |
586 | onRender: function() { | |
587 | this.summary.show(new CRM.Designer.UFFieldSummaryView({ | |
588 | model: this.model, | |
589 | fieldSchema: this.model.getFieldSchema(), | |
590 | paletteFieldModel: this.options.paletteFieldModel | |
591 | })); | |
592 | this.detail.show(new CRM.Designer.UFFieldDetailView({ | |
593 | model: this.model, | |
594 | fieldSchema: this.model.getFieldSchema() | |
595 | })); | |
596 | this.onChangeIsDuplicate(this.model, this.model.get('is_duplicate')) | |
597 | if (!this.expanded) { | |
598 | this.detail.$el.hide(); | |
599 | } | |
600 | var that = this; | |
601 | CRM.designerApp.vent.on('formOpened', function(event) { | |
602 | if (that.expanded && event != that.cid) { | |
603 | that.doToggleForm(false); | |
604 | } | |
605 | }); | |
606 | }, | |
607 | doToggleForm: function(event) { | |
608 | this.expanded = !this.expanded; | |
609 | if (this.expanded && event !== false) { | |
610 | CRM.designerApp.vent.trigger('formOpened', this.cid); | |
611 | } | |
612 | this.$el.toggleClass('crm-designer-open', this.expanded); | |
613 | var $detail = this.detail.$el; | |
614 | if (!this.expanded) { | |
615 | $detail.toggle('blind', 250); | |
2b83aa0d | 616 | this.$('button.crm-designer-edit-custom').remove(); |
6a488035 TO |
617 | } |
618 | else { | |
619 | var $canvas = $('.crm-designer-canvas'); | |
620 | var top = $canvas.offset().top; | |
621 | $detail.slideDown({ | |
622 | duration: 250, | |
623 | step: function(num, effect) { | |
624 | // Scroll canvas to keep field details visible | |
625 | if (effect.prop == 'height') { | |
626 | if (effect.now + $detail.offset().top - top > $canvas.height() - 9) { | |
627 | $canvas.scrollTop($canvas.scrollTop() + effect.now + $detail.offset().top - top - $canvas.height() + 9); | |
628 | } | |
629 | } | |
630 | } | |
631 | }); | |
2b83aa0d | 632 | if (this.model.get('field_name').split('_')[0] == 'custom') { |
fa3a5fe2 CW |
633 | this.$('.crm-designer-field-summary > div').append('<button class="crm-designer-edit-custom">» ' + ts('Edit Custom Field') + '</button>'); |
634 | this.$('button.crm-designer-edit-custom').button().attr('title', ts('Edit global settings for this custom field.')); | |
2b83aa0d | 635 | } |
6a488035 TO |
636 | } |
637 | }, | |
2b83aa0d | 638 | doEditCustomField: function() { |
fa3a5fe2 | 639 | var url = CRM.url('civicrm/admin/custom/group/field/update', { |
2b83aa0d CW |
640 | action: 'update', |
641 | reset: 1, | |
642 | id: this.model.get('field_name').split('_')[1] | |
fa3a5fe2 CW |
643 | }); |
644 | var form1 = CRM.loadForm(url, {openInline: '.crm-custom-field-form-block-data_type a'}) | |
645 | .on('crmFormLoad', function() { | |
2b83aa0d | 646 | $(this).prepend('<div class="messages status"><div class="icon inform-icon"></div>' + ts('Note: This will modify the field system-wide, not just in this profile form.') + '</div>'); |
fa3a5fe2 CW |
647 | var $link = $('.action-link a', this); |
648 | if ($link.length) { | |
649 | $link.detach(); | |
650 | var buttons = {}; | |
651 | buttons[$link.text()] = function() { | |
652 | var form2 = CRM.loadForm($link.attr('href'), { | |
653 | cancelButton: '.cancel.form-submit, #done', | |
134bc82c | 654 | openInline: 'a.action-item:not([href="#"])', |
fa3a5fe2 CW |
655 | dialog: { |
656 | width: '60%', | |
657 | height: parseInt($(window).height() * .8) | |
658 | } | |
659 | }).on('crmLoad', function() { | |
660 | $('#done', this).removeAttr('onclick'); | |
661 | }); | |
662 | } | |
663 | $(this).dialog('option', 'buttons', buttons); | |
664 | } | |
665 | }) | |
2b83aa0d CW |
666 | return false; |
667 | }, | |
6a488035 TO |
668 | onChangeIsDuplicate: function(model, value, options) { |
669 | this.$el.toggleClass('crm-designer-duplicate', value); | |
670 | }, | |
671 | doRemove: function(event) { | |
672 | var that = this; | |
673 | this.$el.hide(250, function() { | |
674 | that.model.destroyLocal(); | |
675 | }); | |
676 | } | |
677 | }); | |
678 | ||
679 | /** | |
680 | * options: | |
681 | * - model: CRM.UF.UFFieldModel | |
682 | * - fieldSchema: (Backbone.Form schema element) | |
683 | * - paletteFieldModel: CRM.Designer.PaletteFieldModel | |
684 | */ | |
685 | CRM.Designer.UFFieldSummaryView = Backbone.Marionette.ItemView.extend({ | |
686 | serializeData: extendedSerializeData, | |
687 | template: '#field_summary_template', | |
688 | modelEvents: { | |
689 | 'change': 'render' | |
690 | }, | |
691 | ||
692 | /** | |
693 | * Compose a printable string which describes the binding of this UFField to the data model | |
694 | * @return {String} | |
695 | */ | |
696 | getBindingLabel: function() { | |
697 | var result = this.options.paletteFieldModel.getSection().title + ": " + this.options.paletteFieldModel.getLabel(); | |
698 | if (this.options.fieldSchema.civiIsPhone) { | |
699 | result = result + '-' + CRM.PseudoConstant.phoneType[this.model.get('phone_type_id')]; | |
700 | } | |
701 | if (this.options.fieldSchema.civiIsLocation) { | |
702 | var locType = this.model.get('location_type_id') ? CRM.PseudoConstant.locationType[this.model.get('location_type_id')] : ts('Primary'); | |
703 | result = result + ' (' + locType + ')'; | |
704 | } | |
705 | return result; | |
706 | }, | |
707 | ||
708 | /** | |
709 | * Return a string marking if the field is required | |
710 | * @return {String} | |
711 | */ | |
712 | getRequiredMarker: function() { | |
713 | if (this.model.get('is_required') == 1) { | |
714 | return ' <span class="crm-marker">*</span> '; | |
715 | } | |
716 | return ''; | |
717 | }, | |
718 | ||
719 | onRender: function() { | |
720 | this.$el.toggleClass('disabled', this.model.get('is_active') != 1); | |
721 | if (this.model.get("is_reserved") == 1) { | |
722 | this.$('.crm-designer-buttons').hide(); | |
723 | } | |
724 | } | |
725 | }); | |
726 | ||
727 | /** | |
728 | * options: | |
729 | * - model: CRM.UF.UFFieldModel | |
730 | * - fieldSchema: (Backbone.Form schema element) | |
731 | */ | |
732 | CRM.Designer.UFFieldDetailView = Backbone.View.extend({ | |
733 | initialize: function() { | |
734 | // FIXME: hide/display 'in_selector' if 'visibility' is one of the public options | |
735 | var fields = ['location_type_id', 'phone_type_id', 'label', 'is_multi_summary', 'is_required', 'is_view', 'visibility', 'in_selector', 'is_searchable', 'help_pre', 'help_post', 'is_active']; | |
736 | if (! this.options.fieldSchema.civiIsLocation) { | |
737 | fields = _.without(fields, 'location_type_id'); | |
738 | } | |
739 | if (! this.options.fieldSchema.civiIsPhone) { | |
740 | fields = _.without(fields, 'phone_type_id'); | |
741 | } | |
742 | if (!this.options.fieldSchema.civiIsMultiple) { | |
743 | fields = _.without(fields, 'is_multi_summary'); | |
744 | } | |
745 | ||
746 | this.form = new Backbone.Form({ | |
747 | model: this.model, | |
748 | fields: fields | |
749 | }); | |
750 | this.form.on('change', this.onFormChange, this); | |
8fd0e719 | 751 | this.model.on('change', this.onModelChange, this); |
6a488035 TO |
752 | }, |
753 | render: function() { | |
754 | this.$el.html(this.form.render().el); | |
755 | this.onFormChange(); | |
756 | }, | |
8fd0e719 CW |
757 | onModelChange: function() { |
758 | $.each(this.form.fields, function(i, field) { | |
759 | this.form.setValue(field.key, this.model.get(field.key)); | |
760 | }); | |
761 | }, | |
6a488035 TO |
762 | onFormChange: function() { |
763 | this.form.commit(); | |
764 | this.$('.field-is_multi_summary').toggle(this.options.fieldSchema.civiIsMultiple ? true : false); | |
765 | this.$('.field-in_selector').toggle(this.model.isInSelectorAllowed()); | |
6a488035 TO |
766 | |
767 | if (!this.model.isInSelectorAllowed() && this.model.get('in_selector') != "0") { | |
768 | this.model.set('in_selector', "0"); | |
769 | this.form.setValue('in_selector', "0"); | |
770 | // TODO: It might be nicer if we didn't completely discard in_selector -- e.g. | |
771 | // if the value could be restored when the user isInSelectorAllowed becomes true | |
772 | // again. However, I haven't found a simple way to do this. | |
773 | } | |
774 | } | |
775 | }); | |
776 | ||
777 | /** | |
778 | * options: | |
779 | * - model: CRM.UF.UFGroupModel | |
780 | */ | |
781 | CRM.Designer.UFGroupView = Backbone.Marionette.Layout.extend({ | |
782 | serializeData: extendedSerializeData, | |
783 | template: '#form_row_template', | |
784 | expanded: false, | |
785 | regions: { | |
786 | summary: '.crm-designer-form-summary', | |
787 | detail: '.crm-designer-form-detail' | |
788 | }, | |
789 | events: { | |
790 | "click .crm-designer-action-settings": 'doToggleForm' | |
791 | }, | |
792 | onRender: function() { | |
793 | this.summary.show(new CRM.Designer.UFGroupSummaryView({ | |
794 | model: this.model | |
795 | })); | |
796 | this.detail.show(new CRM.Designer.UFGroupDetailView({ | |
797 | model: this.model | |
798 | })); | |
799 | if (!this.expanded) { | |
800 | this.detail.$el.hide(); | |
801 | } | |
802 | var that = this; | |
803 | CRM.designerApp.vent.on('formOpened', function(event) { | |
804 | if (that.expanded && event !== 0) { | |
805 | that.doToggleForm(false); | |
806 | } | |
807 | }); | |
808 | }, | |
809 | doToggleForm: function(event) { | |
810 | this.expanded = !this.expanded; | |
811 | if (this.expanded && event !== false) { | |
812 | CRM.designerApp.vent.trigger('formOpened', 0); | |
813 | } | |
814 | this.$el.toggleClass('crm-designer-open', this.expanded); | |
815 | this.detail.$el.toggle('blind', 250); | |
816 | } | |
817 | }); | |
818 | ||
819 | /** | |
820 | * options: | |
821 | * - model: CRM.UF.UFGroupModel | |
822 | */ | |
823 | CRM.Designer.UFGroupSummaryView = Backbone.Marionette.ItemView.extend({ | |
824 | serializeData: extendedSerializeData, | |
825 | template: '#form_summary_template', | |
826 | modelEvents: { | |
827 | 'change': 'render' | |
828 | }, | |
829 | onRender: function() { | |
830 | this.$el.toggleClass('disabled', this.model.get('is_active') != 1); | |
831 | if (this.model.get("is_reserved") == 1) { | |
832 | this.$('.crm-designer-buttons').hide(); | |
833 | } | |
834 | } | |
835 | }); | |
836 | ||
837 | /** | |
838 | * options: | |
839 | * - model: CRM.UF.UFGroupModel | |
840 | */ | |
841 | CRM.Designer.UFGroupDetailView = Backbone.View.extend({ | |
842 | initialize: function() { | |
843 | this.form = new Backbone.Form({ | |
844 | model: this.model, | |
845 | fields: ['title', 'help_pre', 'help_post', 'is_active'] | |
846 | }); | |
847 | this.form.on('change', this.form.commit, this.form); | |
848 | }, | |
849 | render: function() { | |
850 | this.$el.html(this.form.render().el); | |
851 | } | |
852 | }); | |
853 | ||
854 | })(cj); |