Update of /var/cvs/applications/searchrelate/mmbase/searchrelate
In directory james.mmbase.org:/tmp/cvs-serv7996

Modified Files:
        Searcher.js.jsp 
Log Message:
split up the class into 3 differents ones. MMBaseSearcher (which takes care of 
searching and paging), MMBaseRelater (which takes care of making relations), 
and MMBaseLogger


See also: 
http://cvs.mmbase.org/viewcvs/applications/searchrelate/mmbase/searchrelate


Index: Searcher.js.jsp
===================================================================
RCS file: 
/var/cvs/applications/searchrelate/mmbase/searchrelate/Searcher.js.jsp,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- Searcher.js.jsp     7 Apr 2008 10:13:06 -0000       1.9
+++ Searcher.js.jsp     7 Apr 2008 15:03:18 -0000       1.10
@@ -11,53 +11,23 @@
 
  *
  * @author Michiel Meeuwissen
- * @version $Id: Searcher.js.jsp,v 1.9 2008/04/07 10:13:06 michiel Exp $
+ * @version $Id: Searcher.js.jsp,v 1.10 2008/04/07 15:03:18 michiel Exp $
  */
 
 $(document).ready(function(){
     $("body").find("div.mm_related")
     .each(function() {
-       var parent = this;
-       $(parent).find(".searchable").each(function() {
-           var searchable = this;
-           $(searchable).find("a.search").each(function() {
-               var anchor = this;
-               anchor.searcher = new MMBaseSearcher(searchable, parent);
-               $(anchor).click(function() {
-                   return this.searcher.search(0);
-               });
-               if (anchor.searcher.canUnrelate) {
-               $(parent).find("tr.click").each(function() {
-                   $(this).click(function() {
-                       anchor.searcher.unrelate(this);
-                       return false;
-                   })});
-               }
-           });
-       });
+       this.relater = new MMBaseRelater(this);
     });
 });
 
 
-
-function MMBaseSearcher(d, p) {
-    this.logEnabled   = true;
-    this.traceEnabled = true;
-    this.div = d;
-    this.parent = p;
-    this.value = "";
-    this.canUnrelate = $(p).hasClass("can_unrelate");
-    this.transaction   = null;
-    var self = this;
-    $(d).find("span.transactioname").each(function() {
-       this.transaction = this.nodeValue;
-    });
-    this.searchResults = {};
-    this.related       = {};
-    this.unrelated     = {};
+function MMBaseLogger() {
+    this.logEnabled   = false;
+    this.traceEnabled = false;
 }
 
-MMBaseSearcher.prototype.log = function (msg) {
+MMBaseLogger.prototype.debug = function (msg) {
     if (this.logEnabled) {
         var errorTextArea = document.getElementById(this.logarea);
         if (errorTextArea) {
@@ -70,84 +40,90 @@
 }
 
 
-/**
- * This method is called if somebody clicks on a.search.
- * It depends on a jsp /mmbase/searchrelate/page.jspx to return the search 
results.
- * Feeded are 'id' 'offset' and 'search'.
- * The actual query is supposed to be on the user's session, and will be 
picked up in page.jspx.
- */
-MMBaseSearcher.prototype.search = function(offset) {
-    var newSearch = $(this.div).find("input")[0].value;
-    if (newSearch != this.value) {
-       this.searchResults = {};
-       this.value = newSearch;
-    }
-    var id = this.div.id;
-    var rep = $(this.div).find("> div")[0]
 
-    var url = "${mm:link('/mmbase/searchrelate/page.jspx')}";
-    var params = {id: id, offset: offset, search: this.value};
+function MMBaseRelater(d) {
+    this.div          = d;
+    this.related       = {};
+    this.unrelated     = {};
+    this.logger        = new MMBaseLogger();
+    this.logger.debug("setting up current");
+    this.current       = $(d).find(".mm_relate_current")[0];
+    if (this.current != null) this.addSearcher(this.current, "current");
+    this.logger.debug("setting up repository");
+    this.repository    = $(d).find(".mm_relate_repository")[0];
+    if (this.repository != null) this.addSearcher(this.repository, 
"repository");
+    this.canUnrelate = $(d).hasClass("can_unrelate");
+
 
-    var result = this.searchResults["" + offset];
-    var self = this;
-    if (result == null) {
-       $.ajax({url: url, type: "GET", dataType: "xml", data: params,
-               complete: function(res, status){
-                   if ( status == "success" || status == "notmodified" ) {
-                       var r = $(res.responseText)[0];
-                       self.log(rep);
-                       $(rep).empty();
-                       $(rep).append($(r).find("> *"));
-                       self.searchResults["" + offset] = r;
-                       self.bindEvents(rep);
-                   }
-               }
-              });
-    } else {
-       this.log("reusing " + offset);
-       $(rep).empty();
-       $(rep).append(result);
-       self.bindEvents(rep);
-    }
-    return false;
 }
 
-MMBaseSearcher.prototype.bindEvents = function(rep) {
-    var self = this;
-    $(rep).find("a.navigate").each(function() {
-       $(this).click(function() {
-           return self.search(this.name);
-       })});
-    $(rep).find("tr.click").each(function() {
+
+MMBaseRelater.prototype.addSearcher = function(el, type) {
+    var relater = this;
+    if ($(el).hasClass("searchable")) {
+       $(el).find("a.search").each(function() {
+           console.log(el);
+           console.log("found "+ this);
+           var anchor = this;
+           anchor.searcher = new MMBaseSearcher(el, relater, type, 
relater.logger);
+           $(anchor).click(function(el) {
+               return this.searcher.search(anchor, 0);
+           });
+           if (relater.canUnrelate) {
+               $(parent).find("tr.click").each(function() {
        $(this).click(function() {
-           self.relate(this);
+                       anchor.searcher.unrelate(this);
            return false;
        })});
+           }
+       });
+    }
 }
+
+
 /**
- * Moves a node from the 'unrelated' repository to the list of related nodes.
+ * Commits made changes to MMBase. Depends on a jsp 
/mmbase/searchrelate/relate.jsp to do the actual work.
+*  This jsp, in turn, depends on the query in the user's session which defined 
what precisely must happen.
  */
-MMBaseSearcher.prototype.relate = function(el) {
-    var number = $(el).find("td.node.number")[0].textContent;
-    if (typeof(this.unrelated[number]) == "undefined") {
-       this.related[number] = el;
+
+MMBaseRelater.prototype.commit = function(el) {
+    var a = el.target;
+    $(a).addClass("submitting");
+    $(a).removeClass("failed");
+    $(a).removeClass("succeeded");
+    this.logger.debug("Commiting changed relations of " + this.div.id);
+    var id = this.div.id;
+    var url = "${mm:link('/mmbase/searchrelate/relate.jspx')}";
+
+    var relatedNumbers   = this.getNumbers(this.related);
+    var unrelatedNumbers = this.getNumbers(this.unrelated);
+
+    this.logger.debug("+ " + relatedNumbers);
+    this.logger.debug("- " + unrelatedNumbers);
+    var params = {id: id, related: relatedNumbers, unrelated: 
unrelatedNumbers};
+    if (this.transaction != null) {
+       params.transaction = this.transaction;
+    }
+    $.ajax({async: true, url: url, type: "GET", dataType: "xml", data: params,
+           complete: function(res, status){
+               $(a).removeClass("submitting");
+               if (status == "success") {
+                   //console.log("" + res);
+                   $(a).addClass("succeeded");
+                   return true;
+               } else {
+                   $(a).addClass("failed");
+                   return false;
+               }
     }
-    this.log("Found number to relate " + number + "+" + 
this.getNumbers(this.related));
-    this.unrelated[number] = null;
-    var current =  $(el).parents("div.mm_related").find("div.mm_relate_current 
table.searchresult tbody");
-    this.log(current[0]);
-    $(el).parents("div.mm_related").find("div.mm_relate_current 
table.searchresult tbody").append(el);
-    $(el).unbind();
-    var searcher = this;
-    $(el).click(function() {
-       searcher.unrelate(this);
     });
 }
 
+
 /**
  * Moves a node from the list of related nodes to the 'unrelated' repository.
  */
-MMBaseSearcher.prototype.unrelate = function(el) {
+MMBaseRelater.prototype.unrelate = function(el) {
     var number = $(el).find("td.node.number")[0].textContent;
     if (typeof(this.related[number]) == "undefined") {
        this.unrelated[number] = el;
@@ -162,7 +138,7 @@
 }
 
 
-MMBaseSearcher.prototype.getNumbers = function(map) {
+MMBaseRelater.prototype.getNumbers = function(map) {
     var numbers = "";
     $.each(map, function(key, value) {
        if (value != null) {
@@ -173,45 +149,107 @@
     return numbers;
 }
 
+
+MMBaseRelater.prototype.bindEvents = function(rep, type) {
+    var self = this;
+    $(rep).find("tr.click").each(function() {
+       $(this).click(function() {
+           self.relate(this);
+           return false;
+       })});
+}
 /**
- * Commits made changes to MMBase. Depends on a jsp 
/mmbase/searchrelate/relate.jsp to do the actual work.
-*  This jsp, in turn, depends on the query in the user's session which defined 
what precisely must happen.
+ * Moves a node from the 'unrelated' repository to the list of related nodes.
  */
+MMBaseRelater.prototype.relate = function(el) {
+    var number = $(el).find("td.node.number")[0].textContent;
+    if (typeof(this.unrelated[number]) == "undefined") {
+       this.related[number] = el;
+    }
+    this.logger.debug("Found number to relate " + number + "+" + 
this.getNumbers(this.related));
+    this.unrelated[number] = null;
+    var current =  $(el).parents("div.mm_related").find("div.mm_relate_current 
table.searchresult tbody");
+    this.logger.debug(current[0]);
+    $(el).parents("div.mm_related").find("div.mm_relate_current 
table.searchresult tbody").append(el);
+    $(el).unbind();
+    var searcher = this;
+    $(el).click(function() {
+       searcher.unrelate(this);
+    });
+}
 
-MMBaseSearcher.prototype.commit = function(el) {
-    var a = el.target;
-    $(a).addClass("submitting");
-    $(a).removeClass("failed");
-    $(a).removeClass("succeeded");
-    this.log("Commiting changed relations of " + this.div.id);
-    var id = this.div.id;
-    var url = "${mm:link('/mmbase/searchrelate/relate.jspx')}";
 
-    var relatedNumbers   = this.getNumbers(this.related);
-    var unrelatedNumbers = this.getNumbers(this.unrelated);
 
-    this.log("+ " + relatedNumbers);
-    this.log("- " + unrelatedNumbers);
-    var params = {id: id, related: relatedNumbers, unrelated: 
unrelatedNumbers};
-    if (this.transaction != null) {
-       params.transaction = this.transaction;
+
+
+function MMBaseSearcher(d, r, type, logger) {
+    this.div = d;
+    this.relater = r;
+    this.type    = type;
+    this.logger  = logger != null ? logger : new MMBaseLogger();
+    this.value = "";
+    this.transaction   = null;
+    var self = this;
+    $(d).find("span.transactioname").each(function() {
+       this.transaction = this.nodeValue;
+    });
+    this.searchResults = {};
+}
+
+
+/**
+ * This method is called if somebody clicks on a.search.
+ * It depends on a jsp /mmbase/searchrelate/page.jspx to return the search 
results.
+ * Feeded are 'id' 'offset' and 'search'.
+ * The actual query is supposed to be on the user's session, and will be 
picked up in page.jspx.
+ */
+MMBaseSearcher.prototype.search = function(el, offset) {
+    var newSearch = $(this.div).find("input")[0].value;
+    if (newSearch != this.value) {
+       this.searchResults = {};
+       this.value = newSearch;
     }
-    $.ajax({async: true, url: url, type: "GET", dataType: "xml", data: params,
+    var id = el.href.substring(el.href.indexOf("#") + 1);
+    var rep = $(this.div).find("> div")[0]
+
+    var url = "${mm:link('/mmbase/searchrelate/page.jspx')}";
+    var params = {id: id, offset: offset, search: this.value};
+
+    var result = this.searchResults["" + offset];
+    var self = this;
+    if (result == null) {
+       $.ajax({url: url, type: "GET", dataType: "xml", data: params,
            complete: function(res, status){
-               $(a).removeClass("submitting");
-               if (status == "success") {
-                   //console.log("" + res);
-                   $(a).addClass("succeeded");
-                   return true;
-               } else {
-                   $(a).addClass("failed");
-                   return false;
+                   if ( status == "success" || status == "notmodified" ) {
+                       var r = $(res.responseText)[0];
+                       self.logger.debug(rep);
+                       $(rep).empty();
+                       $(rep).append($(r).find("> *"));
+                       self.searchResults["" + offset] = r;
+                       self.bindEvents(rep);
+
                }
            }
           });
+    } else {
+       this.logger.debug("reusing " + offset);
+       $(rep).empty();
+       $(rep).append(result);
+       self.bindEvents(rep);
+    }
+    return false;
 }
 
-
+MMBaseSearcher.prototype.bindEvents = function(el) {
+    if (this.relater != null) {
+       this.relater.bindEvents(el, this.type);
+    }
+    var self = this;
+    $(el).find("a.navigate").click(function(el) {
+       var anchor = el.target;
+       return self.search(anchor, anchor.name);
+    });
+}
 
 
 </mm:content>
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to