From cfea01168096c78f84b155f9f6e0c336d5c6ed9a Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 18 Mar 2011 10:50:13 +1300 Subject: [PATCH] ENHANCEMENT Fixed tree search --- javascript/CMSMain.js | 25 +++++++++---------------- javascript/LeftAndMain.Tree.js | 30 +++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/javascript/CMSMain.js b/javascript/CMSMain.js index 03c58d52..c2a8c9a8 100644 --- a/javascript/CMSMain.js +++ b/javascript/CMSMain.js @@ -106,20 +106,15 @@ data[el.name] = el.value; }); - // Set new URL - $('#sitetree')[0].setCustomURL(this.attr('action') + '&action_doSearchTree=1', data); - // Disable checkbox tree controls that currently don't work with search. - // @todo: Make them work together - if ($('#sitetree')[0].isDraggable) $('#sitetree')[0].stopBeingDraggable(); - this.find('.checkboxAboveTree :checkbox').val(false).attr('disabled', true); - + this.find('.checkboxAboveTree :checkbox').attr('disabled', 'disabled'); + // disable buttons to avoid multiple submission //this.find(':submit').attr('disabled', true); this.find(':submit[name=action_doSearchTree]').addClass('loading'); - this._reloadSitetree(); + this._reloadSitetree(this.serializeArray()); return false; }, @@ -134,9 +129,6 @@ this.find('.field :input').clearFields(); this.find('.field').not('.show-default').hide(); - // Reset URL to default - $('#sitetree')[0].clearCustomURL(); - // Enable checkbox tree controls this.find('.checkboxAboveTree :checkbox').attr('disabled', 'false'); @@ -171,21 +163,22 @@ /** * Function: _reloadSitetree */ - _reloadSitetree: function() { + _reloadSitetree: function(params) { var self = this; - $('#sitetree')[0].reload({ - onSuccess : function(response) { + $('#sitetree_ul').search( + params, + function() { self.find(':submit').attr('disabled', false).removeClass('loading'); self.find('.checkboxAboveTree :checkbox').attr('disabled', 'true'); statusMessage('Filtered tree','good'); }, - onFailure : function(response) { + function() { self.find(':submit').attr('disabled', false).removeClass('loading'); self.find('.checkboxAboveTree :checkbox').attr('disabled', 'true'); errorMessage('Could not filter site tree
' + response.responseText); } - }); + ); } }); diff --git a/javascript/LeftAndMain.Tree.js b/javascript/LeftAndMain.Tree.js index 8cd44268..ee739968 100755 --- a/javascript/LeftAndMain.Tree.js +++ b/javascript/LeftAndMain.Tree.js @@ -36,6 +36,7 @@ * @todo When new edit form is loaded, automatically: Select matching node, set correct parent, * update icon and title */ + var self = this; this .jstree({ 'core': { @@ -83,17 +84,19 @@ data.inst._set_settings({'html_data': {'ajax': { 'url': self.data('url-tree'), 'data': function(node) { - return $.extend( - self.data('searchparams') || {}, - { ID : $(node).data("id") ? $(node).data("id") : 0 , ajax: 1} - ); + var params = self.data('searchparams') || []; + // Avoid duplication of parameters + params = $.grep(params, function(n, i) {return (n.name != 'ID' && n.name != 'value');}); + params.push({name: 'ID', value: $(node).data("id") ? $(node).data("id") : 0}); + params.push({name: 'ajax', value: 1}); + return params; } }}}); }) .bind('before.jstree', function(e, data) { if(data.func == 'start_drag') { - // Only allow drag'n'drop if it has been specifically enabled - if(!$('input[id=sortitems]').is(':checked')) { + // Only allow drag'n'drop if it has been specifically enabled, or the tree is in search mode + if(!$('input[id=sortitems]').is(':checked') || self.data('searchparams')) { e.stopImmediatePropagation(); return false; } @@ -133,12 +136,25 @@ }); }); - var self = this; $('#Form_EditForm').bind('loadnewpage', function(e, data) { self._onLoadNewPage(e, data); }); }, + /** + * Function: + * search + * + * Parameters: + * (Object) data Pass empty data to cancel search + * (Function) callback Success callback + */ + search: function(params, callback) { + if(params) this.data('searchparams', params); + else this.removeData('searchparams'); + this.jstree('refresh', -1, callback); + }, + /** * Assumes to be triggered by a form element with the following input fields: * ID, ParentID, TreeTitle (or Title), ClassName