2015-08-14 04:51:42 +02:00
|
|
|
/**
|
|
|
|
* form builder behaviour.
|
|
|
|
*/
|
|
|
|
|
2017-08-24 05:14:39 +02:00
|
|
|
window.jQuery.entwine('ss', ($) => {
|
2017-08-21 02:13:19 +02:00
|
|
|
let stickyHeaderInterval = null;
|
2015-08-20 02:37:00 +02:00
|
|
|
|
2017-08-24 05:14:39 +02:00
|
|
|
$('.uf-field-editor .ss-gridfield-items').entwine({
|
|
|
|
onmatch() {
|
2017-08-21 02:13:19 +02:00
|
|
|
let i = 0;
|
|
|
|
let thisLevel = 0;
|
|
|
|
let depth = 0;
|
|
|
|
const $buttonrow = $('.uf-field-editor .ss-gridfield-buttonrow').addClass('sticky-buttons');
|
2017-08-24 05:14:39 +02:00
|
|
|
const navHeight = $('.cms-content-header.north').first().height()
|
2017-08-21 02:13:19 +02:00
|
|
|
+ parseInt($('.sticky-buttons').css('padding-top'), 10);
|
|
|
|
const fieldEditor = $('.uf-field-editor');
|
2017-06-30 07:11:36 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
this._super();
|
2017-06-30 07:11:36 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
// Loop through all rows and set necessary styles
|
2017-08-24 05:14:39 +02:00
|
|
|
this.find('.ss-gridfield-item').each((i, el) => {
|
|
|
|
switch ($(el).data('class')) {
|
|
|
|
case 'SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFormStep': {
|
2017-08-21 02:13:19 +02:00
|
|
|
depth = 0;
|
|
|
|
return;
|
|
|
|
}
|
2017-08-24 05:14:39 +02:00
|
|
|
case 'SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFieldGroup': {
|
2017-08-21 02:13:19 +02:00
|
|
|
thisLevel = ++depth;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-24 05:14:39 +02:00
|
|
|
case 'SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFieldGroupEnd': {
|
2017-08-21 02:13:19 +02:00
|
|
|
thisLevel = depth--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
thisLevel = depth;
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 02:37:00 +02:00
|
|
|
|
2017-08-24 05:14:39 +02:00
|
|
|
$(el).toggleClass('infieldgroup', thisLevel > 0);
|
2017-08-21 02:13:19 +02:00
|
|
|
for (i = 1; i <= 5; i++) {
|
2017-08-24 05:14:39 +02:00
|
|
|
$(el).toggleClass(`infieldgroup-level-${i}`, thisLevel >= i);
|
2017-08-21 02:13:19 +02:00
|
|
|
}
|
|
|
|
});
|
2015-08-20 02:37:00 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
// Make sure gridfield buttons stick to top of page when user scrolls down
|
|
|
|
stickyHeaderInterval = setInterval(() => {
|
|
|
|
const offsetTop = fieldEditor.offset().top;
|
|
|
|
$buttonrow.width('100%');
|
|
|
|
if (offsetTop > navHeight || offsetTop === 0) {
|
|
|
|
$buttonrow.removeClass('sticky-buttons');
|
|
|
|
} else {
|
|
|
|
$buttonrow.addClass('sticky-buttons');
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
},
|
2017-08-24 05:14:39 +02:00
|
|
|
onunmatch() {
|
2017-08-21 02:13:19 +02:00
|
|
|
this._super();
|
2015-08-17 06:39:16 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
clearInterval(stickyHeaderInterval);
|
|
|
|
},
|
|
|
|
});
|
2015-08-17 06:39:16 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
// When new fields are added.
|
|
|
|
$('.uf-field-editor .ss-gridfield-buttonrow .action').entwine({
|
2017-08-24 05:14:39 +02:00
|
|
|
onclick(e) {
|
2017-08-21 02:13:19 +02:00
|
|
|
this._super(e);
|
2015-08-17 06:39:16 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
this.trigger('addnewinline');
|
|
|
|
},
|
|
|
|
});
|
2015-08-17 06:39:16 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
$('.uf-field-editor').entwine({
|
2017-08-24 05:14:39 +02:00
|
|
|
onmatch() {
|
2017-08-21 02:13:19 +02:00
|
|
|
this._super();
|
2017-06-30 07:11:36 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
// When the 'Add field' button is clicked set a one time listener.
|
|
|
|
// When the GridField is reloaded focus on the newly added field.
|
|
|
|
this.on('addnewinline', () => {
|
|
|
|
this.one('reload', () => {
|
|
|
|
// If fieldgroup, focus on the start marker
|
|
|
|
let $newField = this.find('.ss-gridfield-item').last();
|
|
|
|
let $groupEnd = null;
|
2017-08-24 05:14:39 +02:00
|
|
|
if ($newField.attr('data-class') === 'SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFieldGroupEnd') {
|
2017-08-21 02:13:19 +02:00
|
|
|
$groupEnd = $newField;
|
|
|
|
$groupEnd.prev().find('.col-Title input').focus();
|
|
|
|
$newField = $groupEnd.add($groupEnd.prev());
|
|
|
|
$groupEnd.css('visibility', 'hidden');
|
|
|
|
} else {
|
|
|
|
$newField.find('.col-Title input').focus();
|
|
|
|
}
|
2015-08-20 02:37:00 +02:00
|
|
|
|
2017-08-21 02:13:19 +02:00
|
|
|
$newField.addClass('flashBackground');
|
|
|
|
$('.cms-content-fields').scrollTop($('.cms-content-fields')[0].scrollHeight);
|
|
|
|
if ($groupEnd) {
|
|
|
|
$groupEnd.css('visibility', 'visible');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-08-24 05:14:39 +02:00
|
|
|
onummatch() {
|
2017-08-21 02:13:19 +02:00
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|