Forum closed. New forum available at http://community.wymeditor.org/

Improved findUp()

Discuss features, code, contributions, ideas, suggestions, ...
For bugs, patches and feature requests, please post on the Trac:
http://trac.wymeditor.org/

Improved findUp()

Postby strykker on Wed Jan 21, 2009 7:01 am

I needed to add some special container classes which were 'contained' by divs. Different containers were identified by different div ids. The container() function uses the findUp() function to determine if a selection is in a container from an array element names. I needed to be able to determine containers not just by element name, but by other properties as well (in this case, by the class attribute). To do this, I modified the findUp() method to use jQuery selection and added my div.className to the list of valid containers.

This works well for me on firefox and safari. I have not tested on other browsers. I am posting my updated findUp() method here, if it warks across the board, it would be a useful addition.

Code: Select all
/* @name findUp
* @description Returns the first parent or self container, based on its type
*/
WYMeditor.editor.prototype.findUp = function(node, filter) {

  //filter is a string or an array of strings
  if(node) {
      if(typeof(filter) == WYMeditor.STRING) {
      var jNode = jQuery(filter,jQuery(node.parentNode));
      if(jNode.length > 0 && jNode[0] == node) {   //match current node
         return(node);
      } else {
         var jNode = jQuery(node).parents(filter + ":first");
         if(jNode.length > 0) {
            return(jNode[0]);
         }
      }
      return(null);
      } else {
        var bFound = false;
        var i = 0;
      while(!bFound && i < filter.length) {
         var resultNode = this.findUp(node, filter[i]);
         if(resultNode) {
            return (resultNode);
         }
         i++;
      }
      return(null);
      }
  } else return(null);
};


-Eric
strykker
 
Posts: 6
Joined: Wed Jan 21, 2009 6:49 am

Re: Improved findUp()

Postby strykker on Wed Jan 21, 2009 1:36 pm

Oops, there's a bug in determining if the selected node matches the filter itself, here's the fixed code:

Code: Select all
/* @name findUp
* @description Returns the first parent or self container, based on its type
*/
WYMeditor.editor.prototype.findUp = function(node, filter) {

  //filter is a string or an array of strings
  if(node) {

      if(typeof(filter) == WYMeditor.STRING) {

      if(jQuery(node).is(filter)){
         return node;
      } else {
         var jNode = jQuery(node).parents(filter + ":first");
         if(jNode.length > 0) {
            return(jNode[0]);
         }
      }

      return null;

      } else {
     
        var bFound = false;
        var i = 0;
      while(!bFound && i < filter.length) {
         var resultNode = this.findUp(node, filter[i]);
         if(resultNode) {
            return (resultNode);
         }
         i++;
      }
      return null;
      }
  } else return null;
};


-Eric
strykker
 
Posts: 6
Joined: Wed Jan 21, 2009 6:49 am


Return to Developers

Who is online

Users browsing this forum: No registered users and 2 guests