From 82748a0873718c4be839a369fbb26c5e7b8aea9d Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Fri, 17 Feb 2012 12:38:21 +1300 Subject: [PATCH] MINOR JS functionality on intercepting injected filtering of gridfields in LeftAndMain inherited Controllers. --- admin/javascript/LeftAndMain.js | 125 +++++++++++++++++++++++++++++++- javascript/GridField.js | 42 +---------- 2 files changed, 126 insertions(+), 41 deletions(-) diff --git a/admin/javascript/LeftAndMain.js b/admin/javascript/LeftAndMain.js index bff3509ed..f755db8c4 100644 --- a/admin/javascript/LeftAndMain.js +++ b/admin/javascript/LeftAndMain.js @@ -382,7 +382,130 @@ jQuery.noConflict(); }); } }); - }); + }); + + $('.cms-filter-form').entwine({ + + GridField: null, + + /** + * Function onmatch + * + * Try to find the related gridfield by looking up the data-gridfield attribute on this + * filter form + */ + onmatch: function() { + var gridfieldName = this.attr('data-gridfield'); + this.setGridField($('.grid[data-name='+gridfieldName+']')); + var self = this; + this.getGridField().bind('reload', function(e, gridfield){ + self.setFilterValues($(gridfield).getState()); + }); + }, + + /** + * Function: onsubmit + * + * Parameters: + * (Event) e + */ + onsubmit: function(e) { + this.changeState(jQuery(this).find(':submit:first')); + return false; + }, + + + /** + * Function: setFilterValues + * + * Parameters: + * (JSON) state + * + */ + setFilterValues: function(state){ + var filterValues = state.GridFieldFilter.Columns; + if(jQuery.isEmptyObject(filterValues)){ + this.resetFilterForm(); + return; + } + this.filterFields().each(function(idx, element) { + if(typeof filterValues[element.name] !== "undefined") { + $(element).val(filterValues[element.name]); + } + }); + }, + + /** + * Function: onreset + * + * Parameters: + * (Event) e + */ + onreset: function(e) { + if(this.resetFilterForm()) { + this.changeState(jQuery(this)); + } + return false; + }, + + /** + * Function resetFilterForm + * + **/ + resetFilterForm: function() { + var needUpdate = false; + this.filterFields().each(function(idx, element) { + if($(element).val()) { + needUpdate = true; + $(element).val(''); + } + if($(element).hasClass('chzn-done')){ + $(element).trigger("liszt:updated"); + } + }); + return needUpdate; + }, + + /** + * Function: changeState + * + * Change the state of the gridfield, reloads it's and set loading classes on elements + * + * Parameters: + * (Element) element - the element that will get a loading class added / removed + * + */ + changeState: function(element) { + element.addClass('loading'); + this.getGridField().setState('GridFieldFilter', {'Columns': this.filterValues()}); + this.getGridField().reload(null, function(){ + element.removeClass('loading'); + }); + }, + + /** + * Function filterFields + * Get all fields that contains filter values + * + */ + filterFields: function() { + return this.find(':input').not(".action, .hidden"); + }, + + /** + * Function: filterValues + * + * Returns an key-value array for the filter values set in the filter form + * + */ + filterValues: function() { + var filterColumns = {}; + this.filterFields().each(function(idx,elm){ + filterColumns[$(elm).attr('name')] = $(elm).val(); + }); + return filterColumns; + } + }); }(jQuery)); // Backwards compatibility diff --git a/javascript/GridField.js b/javascript/GridField.js index 15ec4c2b7..270141966 100644 --- a/javascript/GridField.js +++ b/javascript/GridField.js @@ -3,6 +3,7 @@ $('fieldset.ss-gridfield').entwine({ /** * @param {Object} Additional options for jQuery.ajax() call + * @param {successCallback} callback to call after reloading succeeded. */ reload: function(ajaxOpts, successCallback) { var self = this, form = this.closest('form'), data = form.find(':input').serializeArray(); @@ -25,6 +26,7 @@ form.removeClass('loading'); if(successCallback) successCallback.apply(this, arguments); + self.trigger('reload', self); }, error: function(e) { alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION', 'An error occured while fetching data from the server\n Please try again later.')); @@ -65,46 +67,6 @@ } }); - /* - * Upon focusing on a filter element, move "filter" and "reset" buttons and display next to the current element - * ToDo ensure filter-button state is maintained after filtering (see resetState param) - * ToDo get working in IE 6-7 - */ - $('fieldset.ss-gridfield input.ss-gridfield-sort').entwine({ - onfocusin: function(e) { - // Dodgy results in IE <=7 & ignore if only one filter-field - countfields = $('fieldset.ss-gridfield input.ss-gridfield-sort').length; - if(($.browser.msie && $.browser.version <= 7) || countfields == 1) { - return false; - } - var eleInput = $(this); - - // Remove existing
and