2012-02-09 11:17:09 +01:00
|
|
|
(function($){
|
2012-02-07 20:56:40 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$.entwine('ss', function($) {
|
|
|
|
$('.ss-gridfield').entwine({
|
|
|
|
/**
|
|
|
|
* @param {Object} Additional options for jQuery.ajax() call
|
|
|
|
* @param {successCallback} callback to call after reloading succeeded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
reload: function(ajaxOpts, successCallback) {
|
2014-07-14 01:15:38 +02:00
|
|
|
var self = this, form = this.closest('form'),
|
2012-05-11 05:31:12 +02:00
|
|
|
focusedElName = this.find(':input:focus').attr('name'), // Save focused element for restoring after refresh
|
|
|
|
data = form.find(':input').serializeArray();
|
|
|
|
|
|
|
|
if(!ajaxOpts) ajaxOpts = {};
|
|
|
|
if(!ajaxOpts.data) ajaxOpts.data = [];
|
|
|
|
ajaxOpts.data = ajaxOpts.data.concat(data);
|
|
|
|
|
|
|
|
|
|
|
|
// Include any GET parameters from the current URL, as the view state might depend on it.
|
|
|
|
// For example, a list prefiltered through external search criteria might be passed to GridField.
|
|
|
|
if(window.location.search) {
|
|
|
|
ajaxOpts.data = window.location.search.replace(/^\?/, '') + '&' + $.param(ajaxOpts.data);
|
|
|
|
}
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2014-07-14 01:15:38 +02:00
|
|
|
// For browsers which do not support history.pushState like IE9, ss framework uses hash to track
|
2014-05-22 04:17:33 +02:00
|
|
|
// the current location for PJAX, so for them we pass the query string stored in the hash instead
|
|
|
|
if(!window.history || !window.history.pushState){
|
|
|
|
if(window.location.hash && window.location.hash.indexOf('?') != -1){
|
|
|
|
ajaxOpts.data = window.location.hash.substring(window.location.hash.indexOf('?') + 1) + '&' + $.param(ajaxOpts.data);
|
|
|
|
}
|
|
|
|
}
|
2012-05-16 02:18:23 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
form.addClass('loading');
|
|
|
|
|
|
|
|
$.ajax($.extend({}, {
|
|
|
|
headers: {"X-Pjax" : 'CurrentField'},
|
|
|
|
type: "POST",
|
|
|
|
url: this.data('url'),
|
|
|
|
dataType: 'html',
|
|
|
|
success: function(data) {
|
|
|
|
// Replace the grid field with response, not the form.
|
|
|
|
// TODO Only replaces all its children, to avoid replacing the current scope
|
|
|
|
// of the executing method. Means that it doesn't retrigger the onmatch() on the main container.
|
|
|
|
self.empty().append($(data).children());
|
|
|
|
|
|
|
|
// Refocus previously focused element. Useful e.g. for finding+adding
|
|
|
|
// multiple relationships via keyboard.
|
|
|
|
if(focusedElName) self.find(':input[name="' + focusedElName + '"]').focus();
|
|
|
|
|
2014-07-14 01:15:38 +02:00
|
|
|
// Update filter
|
2014-05-15 04:15:13 +02:00
|
|
|
if(self.find('.filter-header').length) {
|
|
|
|
var content;
|
|
|
|
if(ajaxOpts.data[0].filter=="show") {
|
|
|
|
content = '<span class="non-sortable"></span>';
|
2014-07-14 01:15:38 +02:00
|
|
|
self.addClass('show-filter').find('.filter-header').show();
|
2014-05-15 04:15:13 +02:00
|
|
|
} else {
|
2015-10-05 17:26:21 +02:00
|
|
|
content = '<button type="button" name="showFilter" class="ss-gridfield-button-filter trigger"></button>';
|
2014-07-14 01:15:38 +02:00
|
|
|
self.removeClass('show-filter').find('.filter-header').hide();
|
2014-05-15 04:15:13 +02:00
|
|
|
}
|
2012-03-06 13:43:00 +01:00
|
|
|
|
2014-05-15 04:15:13 +02:00
|
|
|
self.find('.sortable-header th:last').html(content);
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
2012-02-09 11:17:09 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
form.removeClass('loading');
|
|
|
|
if(successCallback) successCallback.apply(this, arguments);
|
|
|
|
self.trigger('reload', self);
|
|
|
|
},
|
|
|
|
error: function(e) {
|
|
|
|
alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION'));
|
|
|
|
form.removeClass('loading');
|
|
|
|
}
|
|
|
|
}, ajaxOpts));
|
|
|
|
},
|
|
|
|
showDetailView: function(url) {
|
|
|
|
window.location.href = url;
|
|
|
|
},
|
|
|
|
getItems: function() {
|
|
|
|
return this.find('.ss-gridfield-item');
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {String}
|
|
|
|
* @param {Mixed}
|
|
|
|
*/
|
|
|
|
setState: function(k, v) {
|
|
|
|
var state = this.getState();
|
|
|
|
state[k] = v;
|
|
|
|
this.find(':input[name="' + this.data('name') + '[GridState]"]').val(JSON.stringify(state));
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getState: function() {
|
|
|
|
return JSON.parse(this.find(':input[name="' + this.data('name') + '[GridState]"]').val());
|
|
|
|
}
|
|
|
|
});
|
2012-05-16 02:18:23 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield *').entwine({
|
|
|
|
getGridField: function() {
|
|
|
|
return this.closest('.ss-gridfield');
|
2012-03-02 20:27:13 +01:00
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2012-03-02 20:27:13 +01:00
|
|
|
|
2012-05-16 02:18:23 +02:00
|
|
|
|
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield :button[name=showFilter]').entwine({
|
2014-07-14 01:15:38 +02:00
|
|
|
onclick: function(e) {
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.filter-header')
|
|
|
|
.show('slow') // animate visibility
|
|
|
|
.find(':input:first').focus(); // focus first search field
|
|
|
|
this.closest('.ss-gridfield').addClass('show-filter');
|
|
|
|
this.parent().html('<span class="non-sortable"></span>');
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2012-02-07 20:56:40 +01:00
|
|
|
|
2012-03-01 00:19:22 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield .ss-gridfield-item').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
if($(e.target).closest('.action').length) {
|
|
|
|
this._super(e);
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-16 02:18:23 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
var editLink = this.find('.edit-link');
|
|
|
|
if(editLink.length) this.getGridField().showDetailView(editLink.prop('href'));
|
|
|
|
},
|
|
|
|
onmouseover: function() {
|
|
|
|
if(this.find('.edit-link').length) this.css('cursor', 'pointer');
|
|
|
|
},
|
|
|
|
onmouseout: function() {
|
|
|
|
this.css('cursor', 'default');
|
|
|
|
}
|
|
|
|
});
|
2012-05-16 02:18:23 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield .action').entwine({
|
|
|
|
onclick: function(e){
|
|
|
|
var filterState='show'; //filterstate should equal current state.
|
2015-03-30 04:53:08 +02:00
|
|
|
|
|
|
|
// If the button is disabled, do nothing.
|
|
|
|
if (this.button('option', 'disabled')) {
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
if(this.hasClass('ss-gridfield-button-close') || !(this.closest('.ss-gridfield').hasClass('show-filter'))){
|
|
|
|
filterState='hidden';
|
|
|
|
}
|
2012-05-16 02:18:23 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
this.getGridField().reload({data: [{name: this.attr('name'), value: this.val(), filter: filterState}]});
|
|
|
|
e.preventDefault();
|
2016-02-26 03:49:28 +01:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Get the url this action should submit to
|
|
|
|
*/
|
|
|
|
actionurl: function() {
|
|
|
|
var btn = this.closest(':button'), grid = this.getGridField(),
|
|
|
|
form = this.closest('form'), data = form.find(':input.gridstate').serialize(),
|
|
|
|
csrf = form.find('input[name="SecurityID"]').val();
|
|
|
|
|
|
|
|
// Add current button
|
|
|
|
data += "&" + encodeURIComponent(btn.attr('name')) + '=' + encodeURIComponent(btn.val());
|
|
|
|
|
|
|
|
// Add csrf
|
|
|
|
if(csrf) {
|
|
|
|
data += "&SecurityID=" + encodeURIComponent(csrf);
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
2016-02-26 03:49:28 +01:00
|
|
|
|
|
|
|
// Include any GET parameters from the current URL, as the view
|
|
|
|
// state might depend on it. For example, a list pre-filtered
|
|
|
|
// through external search criteria might be passed to GridField.
|
|
|
|
if(window.location.search) {
|
|
|
|
data = window.location.search.replace(/^\?/, '') + '&' + data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide whether we should use ? or & to connect the URL
|
|
|
|
var connector = grid.data('url').indexOf('?') == -1 ? '?' : '&';
|
|
|
|
|
|
|
|
return $.path.makeUrlAbsolute(
|
|
|
|
grid.data('url') + connector + data,
|
|
|
|
$('base').attr('href')
|
|
|
|
);
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
2016-02-26 03:49:28 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
|
|
|
|
2015-03-30 04:53:08 +02:00
|
|
|
/**
|
|
|
|
* Don't allow users to submit empty values in grid field auto complete inputs.
|
|
|
|
*/
|
|
|
|
$('.ss-gridfield .add-existing-autocompleter').entwine({
|
|
|
|
onbuttoncreate: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.toggleDisabled();
|
|
|
|
|
|
|
|
this.find('input[type="text"]').on('keyup', function () {
|
|
|
|
self.toggleDisabled();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onunmatch: function () {
|
|
|
|
this.find('input[type="text"]').off('keyup');
|
|
|
|
},
|
|
|
|
toggleDisabled: function () {
|
|
|
|
var $button = this.find('.ss-ui-button'),
|
|
|
|
$input = this.find('input[type="text"]'),
|
|
|
|
inputHasValue = $input.val() !== '',
|
|
|
|
buttonDisabled = $button.is(':disabled');
|
|
|
|
|
|
|
|
if ((inputHasValue && buttonDisabled) || (!inputHasValue && !buttonDisabled)) {
|
|
|
|
$button.button("option", "disabled", !buttonDisabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-26 05:45:06 +01:00
|
|
|
// Covers both tabular delete button, and the button on the detail form
|
2013-10-07 13:24:16 +02:00
|
|
|
$('.ss-gridfield .col-buttons .action.gridfield-button-delete, .cms-edit-form .Actions button.action.action-delete').entwine({
|
2012-05-11 05:31:12 +02:00
|
|
|
onclick: function(e){
|
|
|
|
if(!confirm(ss.i18n._t('TABLEFIELD.DELETECONFIRMMESSAGE'))) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this._super(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield .action.gridfield-button-print').entwine({
|
|
|
|
UUID: null,
|
|
|
|
onmatch: function() {
|
|
|
|
this._super();
|
|
|
|
this.setUUID(new Date().getTime());
|
|
|
|
},
|
2012-05-14 01:43:36 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-11 05:31:12 +02:00
|
|
|
onclick: function(e){
|
2016-02-26 03:49:28 +01:00
|
|
|
var url = this.actionurl();
|
|
|
|
window.open(url);
|
|
|
|
e.preventDefault();
|
2012-03-06 01:21:34 +01:00
|
|
|
return false;
|
2012-03-01 00:19:22 +01:00
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield-print-iframe').entwine({
|
|
|
|
onmatch: function(){
|
2014-07-14 01:15:38 +02:00
|
|
|
this._super();
|
|
|
|
|
2012-05-14 01:43:36 +02:00
|
|
|
this.hide().bind('load', function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
this.focus();
|
|
|
|
var ifWin = this.contentWindow || this;
|
|
|
|
ifWin.print();
|
2014-07-14 01:15:38 +02:00
|
|
|
});
|
2012-05-14 01:43:36 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2012-05-16 02:18:23 +02:00
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
/**
|
|
|
|
* Prevents actions from causing an ajax reload of the field.
|
2013-03-04 22:30:39 +01:00
|
|
|
*
|
|
|
|
* Useful e.g. for actions which rely on HTTP response headers being
|
|
|
|
* interpreted natively by the browser, like file download triggers.
|
2012-05-11 05:31:12 +02:00
|
|
|
*/
|
|
|
|
$('.ss-gridfield .action.no-ajax').entwine({
|
|
|
|
onclick: function(e){
|
2016-02-26 03:49:28 +01:00
|
|
|
window.location.href = this.actionurl();
|
|
|
|
e.preventDefault();
|
2012-03-06 01:21:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2012-03-06 13:36:33 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield .action-detail').entwine({
|
|
|
|
onclick: function() {
|
|
|
|
this.getGridField().showDetailView($(this).prop('href'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-02-07 20:56:40 +01:00
|
|
|
/**
|
2012-05-11 05:31:12 +02:00
|
|
|
* Allows selection of one or more rows in the grid field.
|
|
|
|
* Purely clientside at the moment.
|
2012-02-07 20:56:40 +01:00
|
|
|
*/
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield[data-selectable]').entwine({
|
|
|
|
/**
|
|
|
|
* @return {jQuery} Collection
|
|
|
|
*/
|
|
|
|
getSelectedItems: function() {
|
|
|
|
return this.find('.ss-gridfield-item.ui-selected');
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @return {Array} Of record IDs
|
|
|
|
*/
|
|
|
|
getSelectedIDs: function() {
|
|
|
|
return $.map(this.getSelectedItems(), function(el) {return $(el).data('id');});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$('.ss-gridfield[data-selectable] .ss-gridfield-items').entwine({
|
2014-07-14 01:15:38 +02:00
|
|
|
onadd: function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
this._super();
|
2014-07-14 01:15:38 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
// TODO Limit to single selection
|
|
|
|
this.selectable();
|
|
|
|
},
|
2014-07-14 01:15:38 +02:00
|
|
|
onremove: function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
this._super();
|
2014-07-14 01:15:38 +02:00
|
|
|
if (this.data('selectable')) this.selectable('destroy');
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
|
|
|
});
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-02-07 20:56:40 +01:00
|
|
|
/**
|
2012-05-11 05:31:12 +02:00
|
|
|
* Catch submission event in filter input fields, and submit the correct button
|
|
|
|
* rather than the whole form.
|
2012-02-07 20:56:40 +01:00
|
|
|
*/
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.ss-gridfield .filter-header :input').entwine({
|
2012-05-14 01:43:36 +02:00
|
|
|
onmatch: function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
var filterbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-filter'),
|
|
|
|
resetbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-reset');
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
if(this.val()) {
|
|
|
|
filterbtn.addClass('filtered');
|
|
|
|
resetbtn.addClass('filtered');
|
|
|
|
}
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2012-05-11 05:31:12 +02:00
|
|
|
},
|
2012-05-14 01:43:36 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-11 05:31:12 +02:00
|
|
|
onkeydown: function(e) {
|
|
|
|
// Skip reset button events, they should trigger default submission
|
|
|
|
if(this.closest('.ss-gridfield-button-reset').length) return;
|
|
|
|
|
|
|
|
var filterbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-filter'),
|
|
|
|
resetbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-reset');
|
|
|
|
|
|
|
|
if(e.keyCode == '13') {
|
|
|
|
var btns = this.closest('.filter-header').find('.ss-gridfield-button-filter');
|
2016-02-26 05:45:06 +01:00
|
|
|
var filterState='show'; //filterstate should equal current state.
|
2012-05-11 05:31:12 +02:00
|
|
|
if(this.hasClass('ss-gridfield-button-close')||!(this.closest('.ss-gridfield').hasClass('show-filter'))){
|
|
|
|
filterState='hidden';
|
|
|
|
}
|
2016-02-26 05:45:06 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
this.getGridField().reload({data: [{name: btns.attr('name'), value: btns.val(), filter: filterState}]});
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
filterbtn.addClass('hover-alike');
|
|
|
|
resetbtn.addClass('hover-alike');
|
|
|
|
}
|
2012-03-01 12:25:44 +01:00
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".ss-gridfield .relation-search").entwine({
|
|
|
|
onfocusin: function (event) {
|
|
|
|
this.autocomplete({
|
|
|
|
source: function(request, response){
|
|
|
|
var searchField = $(this.element);
|
|
|
|
var form = $(this.element).closest("form");
|
|
|
|
$.ajax({
|
|
|
|
headers: {
|
|
|
|
"X-Pjax" : 'Partial'
|
|
|
|
},
|
|
|
|
type: "GET",
|
2013-09-27 19:51:32 +02:00
|
|
|
url: $(searchField).data('searchUrl'),
|
2016-02-26 05:45:06 +01:00
|
|
|
data: encodeURIComponent(searchField.attr('name'))+'='+encodeURIComponent(searchField.val()),
|
2012-05-11 05:31:12 +02:00
|
|
|
success: function(data) {
|
|
|
|
response( $.map(JSON.parse(data), function( name, id ) {
|
|
|
|
return { label: name, value: name, id: id };
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
error: function(e) {
|
|
|
|
alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION', 'An error occured while fetching data from the server\n Please try again later.'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
select: function(event, ui) {
|
2016-05-02 07:37:03 +02:00
|
|
|
var hiddenField = $('<input type="hidden" name="relationID" class="action_gridfield_relationfind" />');
|
|
|
|
hiddenField.val(ui.item.id);
|
|
|
|
$(this)
|
|
|
|
.closest(".ss-gridfield")
|
|
|
|
.find(".action_gridfield_relationfind")
|
|
|
|
.replaceWith(hiddenField);
|
|
|
|
var addbutton = $(this).closest(".ss-gridfield").find(".action_gridfield_relationadd");
|
2012-05-11 05:31:12 +02:00
|
|
|
if(addbutton.data('button')){
|
|
|
|
addbutton.button('enable');
|
|
|
|
}else{
|
|
|
|
addbutton.removeAttr('disabled');
|
2012-03-06 15:58:34 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-03-06 15:58:34 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$(".ss-gridfield .pagination-page-number input").entwine({
|
|
|
|
onkeydown: function(event) {
|
|
|
|
if(event.keyCode == 13) {
|
|
|
|
var newpage = parseInt($(this).val(), 10);
|
2012-03-08 02:11:09 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
var gridfield = $(this).getGridField();
|
|
|
|
gridfield.setState('GridFieldPaginator', {currentPage: newpage});
|
|
|
|
gridfield.reload();
|
2012-03-08 02:11:09 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-08 02:11:09 +01:00
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2012-03-08 02:11:09 +01:00
|
|
|
});
|
2013-05-15 05:39:28 +02:00
|
|
|
}(jQuery));
|