2 if (!CRM
.Designer
) CRM
.Designer
= {};
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
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").
16 var extendedSerializeData = function() {
17 var result
= Marionette
.ItemView
.prototype.serializeData
.apply(this);
19 result
._model
= this.model
;
20 result
._collection
= this.collection
;
21 result
._options
= this.options
;
26 * Display a dialog window with an editable form for a UFGroupModel
28 * The implementation here is very "jQuery-style" and not "Backbone-style";
32 * - model: CRM.UF.UFGroupModel
34 CRM
.Designer
.DesignerDialog
= Backbone
.Marionette
.Layout
.extend({
35 serializeData
: extendedSerializeData
,
36 template
: '#designer_dialog_template',
37 className
: 'crm-designer-dialog',
39 designerRegion
: '.crm-designer'
41 /** @var bool whether this dialog is currently open */
43 /** @var bool whether any changes have been made */
45 /** @var obj handle for the CRM.alert containing undo link */
47 /** @var bool whether this dialog is being re-opened by the undo link */
50 initialize: function(options
) {
51 CRM
.designerApp
.vent
.on('ufUnsaved', this.onUfChanged
, this);
52 CRM
.designerApp
.vent
.on('ufSaved', this.onUfSaved
, this);
55 if (this.undoAlert
&& this.undoAlert
.close
) this.undoAlert
.close();
56 CRM
.designerApp
.vent
.off('ufUnsaved', this.onUfChanged
, this);
58 onUfChanged: function(isUfUnsaved
) {
59 this.isUfUnsaved
= isUfUnsaved
;
61 onUfSaved: function() {
62 CRM
.designerApp
.vent
.off('ufUnsaved', this.onUfChanged
, this);
63 this.isUfUnsaved
= false;
65 onRender: function() {
66 var designerDialog
= this;
67 designerDialog
.$el
.dialog({
68 autoOpen
: true, // note: affects accordion height
69 title
: ts('Edit Profile'),
72 height
: parseInt($(window
).height() * 0.8, 10),
74 minHeight
: 600, // to allow dropping in big whitespace, coordinate with min-height of .crm-designer-fields
76 // Prevent conflicts with other onbeforeunload handlers
77 designerDialog
.oldOnBeforeUnload
= window
.onbeforeunload
;
78 // Warn of unsaved changes when navigating away from the page
79 window
.onbeforeunload = function() {
80 if (designerDialog
.isDialogOpen
&& designerDialog
.isUfUnsaved
) {
81 return ts("Your profile has not been saved.");
83 if (designerDialog
.oldOnBeforeUnload
) {
84 return designerDialog
.oldOnBeforeUnload
.apply(arguments
);
87 if (designerDialog
.undoAlert
&& designerDialog
.undoAlert
.close
) designerDialog
.undoAlert
.close();
88 designerDialog
.isDialogOpen
= true;
89 // Initialize new dialog if we are not re-opening unsaved changes
90 if (designerDialog
.undoState
=== false) {
91 if (designerDialog
.designerRegion
&& designerDialog
.designerRegion
.close
) designerDialog
.designerRegion
.close();
92 designerDialog
.$el
.block();
93 designerDialog
.options
.findCreateUfGroupModel({
94 onLoad: function(ufGroupModel
) {
95 designerDialog
.model
= ufGroupModel
;
96 var designerLayout
= new CRM
.Designer
.DesignerLayout({
98 el
: '<div class="full-height"></div>'
100 designerDialog
.$el
.unblock();
101 designerDialog
.designerRegion
.show(designerLayout
);
102 CRM
.designerApp
.vent
.trigger('resize');
103 designerDialog
.isUfUnsaved
= false;
107 designerDialog
.undoState
= false;
109 CRM
.designerApp
.DetachedProfiles
= [];
112 window
.onbeforeunload
= designerDialog
.oldOnBeforeUnload
;
113 designerDialog
.isDialogOpen
= false;
115 if (designerDialog
.undoAlert
&& designerDialog
.undoAlert
.close
) designerDialog
.undoAlert
.close();
116 if (designerDialog
.isUfUnsaved
) {
117 designerDialog
.undoAlert
= CRM
.alert('<p>' + ts('%1 has not been saved.', {1: designerDialog
.model
.get('title')}) + '</p><a href="#" class="crm-undo">' + ts('Restore') + '</a>', ts('Unsaved Changes'), 'alert', {expires
: 60000});
118 $('.ui-notify-message a.crm-undo').button({icons
: {primary
: 'ui-icon-arrowreturnthick-1-w'}}).click(function(e
) {
120 designerDialog
.undoState
= true;
121 designerDialog
.$el
.dialog('open');
125 CRM
.designerApp
.restorePreviewArea();
128 CRM
.designerApp
.vent
.trigger('resize');
135 * Display a complete form-editing UI, including canvas, palette, and
139 * - model: CRM.UF.UFGroupModel
141 CRM
.Designer
.DesignerLayout
= Backbone
.Marionette
.Layout
.extend({
142 serializeData
: extendedSerializeData
,
143 template
: '#designer_template',
145 buttons
: '.crm-designer-buttonset-region',
146 palette
: '.crm-designer-palette-region',
147 form
: '.crm-designer-form-region',
148 fields
: '.crm-designer-fields-region'
150 initialize: function() {
151 CRM
.designerApp
.vent
.on('resize', this.onResize
, this);
153 onClose: function() {
154 CRM
.designerApp
.vent
.off('resize', this.onResize
, this);
156 onRender: function() {
157 this.buttons
.show(new CRM
.Designer
.ToolbarView({
160 this.palette
.show(new CRM
.Designer
.PaletteView({
163 this.form
.show(new CRM
.Designer
.UFGroupView({
166 this.fields
.show(new CRM
.Designer
.UFFieldCanvasView({
170 onResize: function() {
171 if (! this.hasResizedBefore
) {
172 this.hasResizedBefore
= true;
173 this.$('.crm-designer-toolbar').resizable({
177 resize: function(event
, ui
) {
178 $('.crm-designer-canvas').css('margin-right', (ui
.size
.width
+ 10) + 'px');
179 $(this).css({left
: '', height
: ''});
181 }).css({left
: '', height
: ''});
187 * Display toolbar with working button
190 * - model: CRM.UF.UFGroupModel
192 CRM
.Designer
.ToolbarView
= Backbone
.Marionette
.ItemView
.extend({
193 serializeData
: extendedSerializeData
,
194 template
: '#designer_buttons_template',
197 'click .crm-designer-save': 'doSave',
198 'click .crm-designer-preview': 'doPreview'
200 onRender: function() {
201 this.$('.crm-designer-save').button({icons
: {primary
: 'ui-icon-check'}}).attr({
202 disabled
: 'disabled',
203 style
: 'opacity:.5; cursor:default;'
205 this.$('.crm-designer-preview').button({icons
: {primary
: 'ui-icon-search'}});
207 initialize: function(options
) {
208 CRM
.designerApp
.vent
.on('ufUnsaved', this.onUfChanged
, this);
210 onUfChanged: function(isUfUnsaved
) {
212 this.$('.crm-designer-save').removeAttr('style').prop('disabled', false);
215 doSave: function(e
) {
217 var ufGroupModel
= this.model
;
218 if (ufGroupModel
.getRel('ufFieldCollection').hasDuplicates()) {
219 CRM
.alert(ts('Please correct errors before saving.'), '', 'alert');
222 var $dialog
= this.$el
.closest('.crm-designer-dialog'); // FIXME use events
224 var profile
= ufGroupModel
.toStrictJSON();
225 profile
["api.UFField.replace"] = {values
: ufGroupModel
.getRel('ufFieldCollection').toSortedJSON(), 'option.autoweight': 0};
226 CRM
.api('UFGroup', 'create', profile
, {
227 success: function(data
) {
231 CRM
.alert(data
.error_message
);
234 _
.each(data
.values
, function(ufGroupResponse
) {
235 if (ufGroupResponse
['api.UFField.replace'].is_error
) {
236 CRM
.alert(ufGroupResponse
['api.UFField.replace'].error_message
);
241 if (!ufGroupModel
.get('id')) {
242 ufGroupModel
.set('id', data
.id
);
244 CRM
.designerApp
.vent
.trigger('ufUnsaved', false);
245 CRM
.designerApp
.vent
.trigger('ufSaved');
246 $dialog
.dialog('close');
251 doPreview: function(e
) {
253 this.previewMode
= !this.previewMode
;
254 if (!this.previewMode
) {
255 $('.crm-designer-preview-canvas').html('');
256 $('.crm-designer-canvas > *, .crm-designer-palette-region').show();
257 $('.crm-designer-preview').button('option', {icons
: {primary
: 'ui-icon-search'}}).find('span').text(ts('Preview'));
260 if (this.model
.getRel('ufFieldCollection').hasDuplicates()) {
261 CRM
.alert(ts('Please correct errors before previewing.'), '', 'alert');
264 var $dialog
= this.$el
.closest('.crm-designer-dialog'); // FIXME use events
267 CRM
.designerApp
.clearPreviewArea();
268 $.post(CRM
.url("civicrm/ajax/inline"), {
269 'qfKey': CRM
.profilePreviewKey
,
270 'class_name': 'CRM_UF_Form_Inline_Preview',
272 'ufData': JSON
.stringify({
273 ufGroup
: this.model
.toStrictJSON(),
274 ufFieldCollection
: this.model
.getRel('ufFieldCollection').toSortedJSON()
276 }).done(function(data
) {
278 $('.crm-designer-canvas > *, .crm-designer-palette-region').hide();
279 $('.crm-designer-preview-canvas').html(data
).show().trigger('crmLoad').find(':input').prop('readOnly', true);
280 $('.crm-designer-preview').button('option', {icons
: {primary
: 'ui-icon-pencil'}}).find('span').text(ts('Edit'));
286 * Display a selection of available fields
289 * - model: CRM.UF.UFGroupModel
291 CRM
.Designer
.PaletteView
= Backbone
.Marionette
.ItemView
.extend({
292 serializeData
: extendedSerializeData
,
293 template
: '#palette_template',
294 el
: '<div class="full-height"></div>',
297 'keyup .crm-designer-palette-search input': 'doSearch',
298 'change .crm-contact-types': 'doSetPaletteEntity',
299 'click .crm-designer-palette-clear-search': 'clearSearch',
300 'click .crm-designer-palette-toggle': 'toggleAll',
301 'click .crm-designer-palette-add button': 'doNewCustomFieldDialog',
302 'click #crm-designer-add-custom-set': 'doNewCustomSetDialog',
303 'dblclick .crm-designer-palette-field': 'doAddToCanvas'
305 initialize: function() {
306 this.model
.getRel('ufFieldCollection')
307 .on('add', this.toggleActive
, this)
308 .on('remove', this.toggleActive
, this);
309 this.model
.getRel('paletteFieldCollection')
310 .on('reset', this.render
, this);
311 CRM
.designerApp
.vent
.on('resize', this.onResize
, this);
313 onClose: function() {
314 this.model
.getRel('ufFieldCollection')
315 .off('add', this.toggleActive
, this)
316 .off('remove', this.toggleActive
, this);
317 this.model
.getRel('paletteFieldCollection')
318 .off('reset', this.render
, this);
319 CRM
.designerApp
.vent
.off('resize', this.onResize
, this);
321 onRender: function() {
322 var paletteView
= this;
324 // Prepare data for jstree
326 var paletteFieldsByEntitySection
= this.model
.getRel('paletteFieldCollection').getFieldsByEntitySection();
328 paletteView
.model
.getRel('ufEntityCollection').each(function(ufEntityModel
){
329 _
.each(ufEntityModel
.getSections(), function(section
, sectionKey
){
330 var defaultValue
= paletteView
.selectedContactType
;
332 defaultValue
= paletteView
.model
.calculateContactEntityType();
335 // set selected option as default, since we are rebuilding palette
336 paletteView
.$('.crm-contact-types').val(defaultValue
).prop('selected','selected');
338 var entitySection
= ufEntityModel
.get('entity_name') + '-' + sectionKey
;
340 if (paletteFieldsByEntitySection
[entitySection
]) {
341 _
.each(paletteFieldsByEntitySection
[entitySection
], function(paletteFieldModel
, k
) {
342 items
.push({data
: paletteFieldModel
.getLabel(), attr
: {'class': 'crm-designer-palette-field', 'data-plm-cid': paletteFieldModel
.cid
}});
345 if (section
.is_addable
) {
346 items
.push({data
: ts('+ Add New Field'), attr
: {'class': 'crm-designer-palette-add'}});
348 if (items
.length
> 0) {
352 state
: _
.contains(paletteView
.openTreeNodes
, sectionKey
) ? 'open' : 'closed',
354 'class': 'crm-designer-palette-section',
355 'data-section': sectionKey
,
356 'data-entity': ufEntityModel
.get('entity_name')
363 this.$('.crm-designer-palette-tree').jstree({
364 'json_data': {data
: treeData
},
366 'case_insensitive' : true,
367 'show_only_matches': true
373 "url": CRM
.config
.resourceBase
+ 'packages/jquery/plugins/jstree/themes/classic/style.css'
375 'plugins': ['themes', 'json_data', 'ui', 'search']
376 }).bind('loaded.jstree', function () {
377 $('.crm-designer-palette-field', this).draggable({
378 appendTo
: '.crm-designer',
379 zIndex
: $(this.$el
).zIndex() + 5000,
381 connectToSortable
: '.crm-designer-fields' // FIXME: tight canvas/palette coupling
383 paletteView
.model
.getRel('ufFieldCollection').each(function(ufFieldModel
) {
384 paletteView
.toggleActive(ufFieldModel
, paletteView
.model
.getRel('ufFieldCollection'));
386 paletteView
.$('.crm-designer-palette-add a').replaceWith('<button>' + $('.crm-designer-palette-add a').first().text() + '</<button>');
387 paletteView
.$('.crm-designer-palette-tree > ul').append('<li><button id="crm-designer-add-custom-set">+ ' + ts('Add Set of Custom Fields') + '</button></li>');
388 paletteView
.$('.crm-designer-palette-tree button').button();
389 }).bind("select_node.jstree", function (e
, data
) {
390 $(this).jstree("toggle_node", data
.rslt
.obj
);
391 $(this).jstree("deselect_node", data
.rslt
.obj
);
394 // FIXME: tight canvas/palette coupling
395 this.$(".crm-designer-fields").droppable({
396 activeClass
: "ui-state-default",
397 hoverClass
: "ui-state-hover",
398 accept
: ":not(.ui-sortable-helper)"
403 onResize: function() {
404 var pos
= this.$('.crm-designer-palette-tree').position();
405 var div
= this.$('.crm-designer-palette-tree').closest('.crm-container').height();
406 this.$('.crm-designer-palette-tree').css({height
: div
- pos
.top
});
408 doSearch: function(e
) {
409 var str
= $(e
.target
).val();
410 this.$('.crm-designer-palette-clear-search').css('visibility', str
? 'visible' : 'hidden');
411 this.$('.crm-designer-palette-tree').jstree("search", str
);
413 doSetPaletteEntity: function(event
) {
414 this.selectedContactType
= $('.crm-contact-types :selected').val();
415 // loop through entity collection and remove non-valid entity section's
416 var newUfEntityModels
= [];
417 this.model
.getRel('ufEntityCollection').each(function(oldUfEntityModel
){
418 var values
= oldUfEntityModel
.toJSON();
419 if (values
.entity_name
== 'contact_1') {
420 values
.entity_type
= $('.crm-contact-types :selected').val();
422 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
424 this.model
.getRel('ufEntityCollection').reset(newUfEntityModels
);
426 doAddToCanvas: function(event
) {
427 var paletteFieldModel
= this.model
.getRel('paletteFieldCollection').get($(event
.currentTarget
).attr('data-plm-cid'));
428 paletteFieldModel
.addToUFCollection(this.model
.getRel('ufFieldCollection'));
429 event
.stopPropagation();
431 doNewCustomFieldDialog: function(e
) {
433 var paletteView
= this;
434 var entityKey
= $(e
.currentTarget
).closest('.crm-designer-palette-section').attr('data-entity');
435 var sectionKey
= $(e
.currentTarget
).closest('.crm-designer-palette-section').attr('data-section');
436 var ufEntityModel
= paletteView
.model
.getRel('ufEntityCollection').getByName(entityKey
);
437 var sections
= ufEntityModel
.getSections();
438 var url
= CRM
.url('civicrm/admin/custom/group/field/add', {
441 gid
: sections
[sectionKey
].custom_group_id
443 CRM
.loadForm(url
).on('crmFormSuccess', function(e
, data
) {
444 paletteView
.doRefresh('custom_' + data
.id
);
447 doNewCustomSetDialog: function(e
) {
449 var paletteView
= this;
450 var url
= CRM
.url('civicrm/admin/custom/group', 'action=add&reset=1');
451 // Create custom field set and automatically go to next step (create fields) after save button is clicked.
452 CRM
.loadForm(url
, {refreshAction
: ['next']})
453 .on('crmFormSuccess', function(e
, data
) {
454 // When form switches to create custom field context, modify button behavior to only continue for "save and new"
455 if (data
.customField
) ($(this).data('civiCrmSnippet').options
.crmForm
.refreshAction
= ['next_new']);
456 paletteView
.doRefresh(data
.customField
? 'custom_' + data
.id
: null);
459 doRefresh: function(fieldToAdd
) {
460 var ufGroupModel
= this.model
;
461 this.getOpenTreeNodes();
462 CRM
.Schema
.reloadModels()
463 .done(function(data
){
464 ufGroupModel
.resetEntities();
466 var field
= ufGroupModel
.getRel('paletteFieldCollection').getFieldByName(null, fieldToAdd
);
467 field
.addToUFCollection(ufGroupModel
.getRel('ufFieldCollection'));
471 CRM
.alert(ts('Failed to retrieve schema'), ts('Error'), 'error');
474 clearSearch: function(e
) {
476 $('.crm-designer-palette-search input').val('').keyup();
478 toggleActive: function(ufFieldModel
, ufFieldCollection
, options
) {
479 var paletteFieldCollection
= this.model
.getRel('paletteFieldCollection');
480 var paletteFieldModel
= paletteFieldCollection
.getFieldByName(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
481 var isAddable
= ufFieldCollection
.isAddable(ufFieldModel
);
482 if (paletteFieldModel
) {
483 this.$('[data-plm-cid='+paletteFieldModel
.cid
+']').toggleClass('disabled', !isAddable
);
486 toggleAll: function(e
) {
487 if (_
.isEmpty($('.crm-designer-palette-search input').val())) {
488 $('.crm-designer-palette-tree').jstree($(e
.target
).attr('rel'));
492 getOpenTreeNodes: function() {
493 var paletteView
= this;
494 this.openTreeNodes
= [];
495 this.$('.crm-designer-palette-section.jstree-open').each(function() {
496 paletteView
.openTreeNodes
.push($(this).data('section'));
502 * Display all UFFieldModel objects in a UFGroupModel.
505 * - model: CRM.UF.UFGroupModel
507 CRM
.Designer
.UFFieldCanvasView
= Backbone
.Marionette
.View
.extend({
508 initialize: function() {
509 this.model
.getRel('ufFieldCollection')
510 .on('add', this.updatePlaceholder
, this)
511 .on('remove', this.updatePlaceholder
, this)
512 .on('add', this.addUFFieldView
, this)
513 .on('reset', this.render
, this);
515 onClose: function() {
516 this.model
.getRel('ufFieldCollection')
517 .off('add', this.updatePlaceholder
, this)
518 .off('remove', this.updatePlaceholder
, this)
519 .off('add', this.addUFFieldView
, this)
520 .off('reset', this.render
, this);
523 var ufFieldCanvasView
= this;
524 this.$el
.html(_
.template($('#field_canvas_view_template').html()));
526 // BOTTOM: Setup field-level editing
527 var $fields
= this.$('.crm-designer-fields');
528 this.updatePlaceholder();
529 var ufFieldModels
= this.model
.getRel('ufFieldCollection').sortBy(function(ufFieldModel
) {
530 return parseInt(ufFieldModel
.get('weight'));
532 _
.each(ufFieldModels
, function(ufFieldModel
) {
533 ufFieldCanvasView
.addUFFieldView(ufFieldModel
, ufFieldCanvasView
.model
.getRel('ufFieldCollection'), {skipWeights
: true});
535 this.$(".crm-designer-fields").sortable({
536 placeholder
: 'crm-designer-row-placeholder',
537 forcePlaceholderSize
: true,
538 receive: function(event
, ui
) {
539 var paletteFieldModel
= ufFieldCanvasView
.model
.getRel('paletteFieldCollection').get(ui
.item
.attr('data-plm-cid'));
540 var ufFieldModel
= paletteFieldModel
.addToUFCollection(
541 ufFieldCanvasView
.model
.getRel('ufFieldCollection'),
544 if (_
.isEmpty(ufFieldModel
)) {
545 ufFieldCanvasView
.$('.crm-designer-fields .ui-draggable').remove();
547 // Move from end to the 'dropped' position
548 var ufFieldViewEl
= ufFieldCanvasView
.$('div[data-field-cid='+ufFieldModel
.cid
+']').parent();
549 ufFieldCanvasView
.$('.crm-designer-fields .ui-draggable').replaceWith(ufFieldViewEl
);
551 // note: the sortable() update callback will call updateWeight
554 ufFieldCanvasView
.updateWeights();
558 /** Determine visual order of fields and set the model values for "weight" */
559 updateWeights: function() {
560 var ufFieldCanvasView
= this;
562 var rows
= this.$('.crm-designer-row').each(function(key
, row
) {
563 if ($(row
).hasClass('placeholder')) {
566 var ufFieldCid
= $(row
).attr('data-field-cid');
567 var ufFieldModel
= ufFieldCanvasView
.model
.getRel('ufFieldCollection').get(ufFieldCid
);
568 ufFieldModel
.set('weight', weight
);
572 addUFFieldView: function(ufFieldModel
, ufFieldCollection
, options
) {
573 var paletteFieldModel
= this.model
.getRel('paletteFieldCollection').getFieldByName(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
574 var ufFieldView
= new CRM
.Designer
.UFFieldView({
575 el
: $("<div></div>"),
577 paletteFieldModel
: paletteFieldModel
579 ufFieldView
.render();
580 this.$('.crm-designer-fields').append(ufFieldView
.$el
);
581 if (! (options
&& options
.skipWeights
)) {
582 this.updateWeights();
585 updatePlaceholder: function() {
586 if (this.model
.getRel('ufFieldCollection').isEmpty()) {
587 this.$('.placeholder').css({display
: 'block', border
: '0 none', cursor
: 'default'});
589 this.$('.placeholder').hide();
596 * - model: CRM.UF.UFFieldModel
597 * - paletteFieldModel: CRM.Designer.PaletteFieldModel
599 CRM
.Designer
.UFFieldView
= Backbone
.Marionette
.Layout
.extend({
600 serializeData
: extendedSerializeData
,
601 template
: '#field_row_template',
604 summary
: '.crm-designer-field-summary',
605 detail
: '.crm-designer-field-detail'
608 "click .crm-designer-action-settings": 'doToggleForm',
609 "click button.crm-designer-edit-custom": 'doEditCustomField',
610 "click .crm-designer-action-remove": 'doRemove'
614 "change:is_duplicate": 'onChangeIsDuplicate'
616 onRender: function() {
617 this.summary
.show(new CRM
.Designer
.UFFieldSummaryView({
619 fieldSchema
: this.model
.getFieldSchema(),
620 paletteFieldModel
: this.options
.paletteFieldModel
622 this.detail
.show(new CRM
.Designer
.UFFieldDetailView({
624 fieldSchema
: this.model
.getFieldSchema()
626 this.onChangeIsDuplicate(this.model
, this.model
.get('is_duplicate'));
627 if (!this.expanded
) {
628 this.detail
.$el
.hide();
631 CRM
.designerApp
.vent
.on('formOpened', function(event
) {
632 if (that
.expanded
&& event
!= that
.cid
) {
633 that
.doToggleForm(false);
637 doToggleForm: function(event
) {
638 this.expanded
= !this.expanded
;
639 if (this.expanded
&& event
!== false) {
640 CRM
.designerApp
.vent
.trigger('formOpened', this.cid
);
642 this.$el
.toggleClass('crm-designer-open', this.expanded
);
643 var $detail
= this.detail
.$el
;
644 if (!this.expanded
) {
645 $detail
.toggle('blind', 250);
646 this.$('button.crm-designer-edit-custom').remove();
649 var $canvas
= $('.crm-designer-canvas');
650 var top
= $canvas
.offset().top
;
653 step: function(num
, effect
) {
654 // Scroll canvas to keep field details visible
655 if (effect
.prop
== 'height') {
656 if (effect
.now
+ $detail
.offset().top
- top
> $canvas
.height() - 9) {
657 $canvas
.scrollTop($canvas
.scrollTop() + effect
.now
+ $detail
.offset().top
- top
- $canvas
.height() + 9);
662 if (this.model
.get('field_name').split('_')[0] == 'custom') {
663 this.$('.crm-designer-field-summary > div').append('<button class="crm-designer-edit-custom">' + ts('Edit Custom Field') + '</button>');
664 this.$('button.crm-designer-edit-custom').button({icons
: {primary
: 'ui-icon-pencil'}}).attr('title', ts('Edit global settings for this custom field.'));
668 doEditCustomField: function(e
) {
670 var url
= CRM
.url('civicrm/admin/custom/group/field/update', {
673 id
: this.model
.get('field_name').split('_')[1]
675 var form1
= CRM
.loadForm(url
)
676 .on('crmFormLoad', function() {
677 $(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>');
680 onChangeIsDuplicate: function(model
, value
, options
) {
681 this.$el
.toggleClass('crm-designer-duplicate', value
);
683 doRemove: function(event
) {
685 this.$el
.hide(250, function() {
686 that
.model
.destroyLocal();
693 * - model: CRM.UF.UFFieldModel
694 * - fieldSchema: (Backbone.Form schema element)
695 * - paletteFieldModel: CRM.Designer.PaletteFieldModel
697 CRM
.Designer
.UFFieldSummaryView
= Backbone
.Marionette
.ItemView
.extend({
698 serializeData
: extendedSerializeData
,
699 template
: '#field_summary_template',
705 * Compose a printable string which describes the binding of this UFField to the data model
708 getBindingLabel: function() {
709 var result
= this.options
.paletteFieldModel
.getSection().title
+ ": " + this.options
.paletteFieldModel
.getLabel();
710 if (this.options
.fieldSchema
.civiIsPhone
) {
711 result
= result
+ '-' + CRM
.PseudoConstant
.phoneType
[this.model
.get('phone_type_id')];
713 if (this.options
.fieldSchema
.civiIsWebsite
) {
714 result
= result
+ '-' + CRM
.PseudoConstant
.websiteType
[this.model
.get('website_type_id')];
716 if (this.options
.fieldSchema
.civiIsLocation
) {
717 var locType
= this.model
.get('location_type_id') ? CRM
.PseudoConstant
.locationType
[this.model
.get('location_type_id')] : ts('Primary');
718 result
= result
+ ' (' + locType
+ ')';
724 * Return a string marking if the field is required
727 getRequiredMarker: function() {
728 if (this.model
.get('is_required') == 1) {
729 return ' <span class="crm-marker">*</span> ';
734 onRender: function() {
735 this.$el
.toggleClass('disabled', this.model
.get('is_active') != 1);
736 if (this.model
.get("is_reserved") == 1) {
737 this.$('.crm-designer-buttons').hide();
744 * - model: CRM.UF.UFFieldModel
745 * - fieldSchema: (Backbone.Form schema element)
747 CRM
.Designer
.UFFieldDetailView
= Backbone
.View
.extend({
748 initialize: function() {
749 // FIXME: hide/display 'in_selector' if 'visibility' is one of the public options
750 var fields
= ['location_type_id', 'website_type_id', 'phone_type_id', 'label', 'is_multi_summary', 'is_required', 'is_view', 'visibility', 'in_selector', 'is_searchable', 'help_pre', 'help_post', 'is_active'];
751 if (! this.options
.fieldSchema
.civiIsLocation
) {
752 fields
= _
.without(fields
, 'location_type_id');
754 if (! this.options
.fieldSchema
.civiIsWebsite
) {
755 fields
= _
.without(fields
, 'website_type_id');
757 if (! this.options
.fieldSchema
.civiIsPhone
) {
758 fields
= _
.without(fields
, 'phone_type_id');
760 if (!this.options
.fieldSchema
.civiIsMultiple
) {
761 fields
= _
.without(fields
, 'is_multi_summary');
764 this.form
= new Backbone
.Form({
768 this.form
.on('change', this.onFormChange
, this);
769 this.model
.on('change', this.onModelChange
, this);
772 this.$el
.html(this.form
.render().el
);
775 onModelChange: function() {
776 $.each(this.form
.fields
, function(i
, field
) {
777 this.form
.setValue(field
.key
, this.model
.get(field
.key
));
780 onFormChange: function() {
782 this.$('.field-is_multi_summary').toggle(this.options
.fieldSchema
.civiIsMultiple
? true : false);
783 this.$('.field-in_selector').toggle(this.model
.isInSelectorAllowed());
785 if (!this.model
.isInSelectorAllowed() && this.model
.get('in_selector') != "0") {
786 this.model
.set('in_selector', "0");
787 this.form
.setValue('in_selector', "0");
788 // TODO: It might be nicer if we didn't completely discard in_selector -- e.g.
789 // if the value could be restored when the user isInSelectorAllowed becomes true
790 // again. However, I haven't found a simple way to do this.
797 * - model: CRM.UF.UFGroupModel
799 CRM
.Designer
.UFGroupView
= Backbone
.Marionette
.Layout
.extend({
800 serializeData
: extendedSerializeData
,
801 template
: '#form_row_template',
804 summary
: '.crm-designer-form-summary',
805 detail
: '.crm-designer-form-detail'
808 "click .crm-designer-action-settings": 'doToggleForm'
810 onRender: function() {
811 this.summary
.show(new CRM
.Designer
.UFGroupSummaryView({
814 this.detail
.show(new CRM
.Designer
.UFGroupDetailView({
817 if (!this.expanded
) {
818 this.detail
.$el
.hide();
821 CRM
.designerApp
.vent
.on('formOpened', function(event
) {
822 if (that
.expanded
&& event
!== 0) {
823 that
.doToggleForm(false);
827 doToggleForm: function(event
) {
828 this.expanded
= !this.expanded
;
829 if (this.expanded
&& event
!== false) {
830 CRM
.designerApp
.vent
.trigger('formOpened', 0);
832 this.$el
.toggleClass('crm-designer-open', this.expanded
);
833 this.detail
.$el
.toggle('blind', 250);
839 * - model: CRM.UF.UFGroupModel
841 CRM
.Designer
.UFGroupSummaryView
= Backbone
.Marionette
.ItemView
.extend({
842 serializeData
: extendedSerializeData
,
843 template
: '#form_summary_template',
847 onRender: function() {
848 this.$el
.toggleClass('disabled', this.model
.get('is_active') != 1);
849 if (this.model
.get("is_reserved") == 1) {
850 this.$('.crm-designer-buttons').hide();
857 * - model: CRM.UF.UFGroupModel
859 CRM
.Designer
.UFGroupDetailView
= Backbone
.View
.extend({
860 initialize: function() {
861 this.form
= new Backbone
.Form({
863 fields
: ['title', 'help_pre', 'help_post', 'is_active']
865 this.form
.on('change', this.form
.commit
, this.form
);
868 this.$el
.html(this.form
.render().el
);