From: Jakub Skoczen Date: Fri, 21 Jan 2011 13:33:30 +0000 (+0100) Subject: Refactor to avoid redundant calls X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=f56694d19c033aa03d48a264705a7481e188957b;p=mkdru-moved-to-drupal.org.git Refactor to avoid redundant calls --- diff --git a/mkdru.client.js b/mkdru.client.js index fff8247..352cae0 100644 --- a/mkdru.client.js +++ b/mkdru.client.js @@ -36,7 +36,7 @@ if (!$.isArray) $.isArray = function(obj) { // pz2.js event handlers: mkdru.pz2Init = function () { if (mkdru.state.query) { - mkdru.triggerSearch(); + mkdru.search(); } //mkdru.pz2.stat(); //mkdru.pz2.bytarget(); @@ -187,36 +187,41 @@ mkdru.hashChange = function () { -// UI functions: +//search bo handler ONLY mkdru.submitQuery = function () { mkdru.state.query = $('.mkdru-search input:text').attr('value'); - mkdru.resetPage(); mkdru.pollDropDowns(); + mkdru.resetPage(); mkdru.hashFromState(); mkdru.search(); mkdru.submitted = true; return false; }; -mkdru.triggerSearch = function () { - mkdru.search(); - mkdru.submitted = true; -}; +//criteria drop-downs (perpage,sort) handler ONLY +mkdru.submitCriteria = function () { + mkdru.pollDropDowns(); + //search is not ON, do nothing + if (!mkdru.submitted) return false; + mkdru.resetPage(); + mkdru.hashFromState(); + mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort); + return false; +} mkdru.search = function () { mkdru.pz2.search(mkdru.state.query, mkdru.state.perpage, mkdru.state.sort, mkdru.state.filter); + //inform others that the search is ON + mkdru.submitted = true; }; mkdru.pollDropDowns = function () { mkdru.state.perpage = $('.mkdru-perpage').attr('value'); mkdru.state.sort = $('.mkdru-sort').attr('value'); - if (!mkdru.submitted) return false; - mkdru.resetPage(); - mkdru.hashFromState(); - return false; }; +//TODO it's rude to mess with user's query, we should have a breadcrumb mkdru.limitQuery = function (field, value) { $('.mkdru-search input:text').attr('value', function () { return this.value += ' and ' + field + '="' + value + '"'; @@ -231,8 +236,9 @@ mkdru.limitTarget = function (id, name) { + name + ''; navi.innerHTML += '
'; mkdru.state.filter = 'pz:id=' + id; - mkdru.resetPage(); mkdru.pollDropDowns(); + mkdru.resetPage(); + mkdru.hashFromState(); mkdru.search(); return false; }; @@ -282,14 +288,8 @@ $(document).ready(function () { $(window).bind( 'hashchange', mkdru.hashChange); $('.mkdru-search').bind('submit', mkdru.submitQuery); $('.mkdru-search input:text').attr('value', ''); - $('.mkdu-perpage').bind('change', function () { - mkdru.pollDropDowns(); - mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort); - }); - $('.mkdru-sort').bind('change', function () { - mkdru.pollDropDowns(); - mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort); - }); + $('.mkdru-perpage').bind('change', mkdru.submitCriteria); + $('.mkdru-sort').bind('change', mkdru.submitCriteria); mkdru.pz2 = new pz2( { "onshow": mkdru.pz2Show, "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way @@ -321,6 +321,6 @@ $(document).ready(function () { mkdru.pz2.record(mkdru.state.recid); } else if (mkdru.state.query) { - mkdru.triggerSearch(); + mkdru.search(); } });