Changeset 1289


Ignore:
Timestamp:
11/20/10 23:29:06 (6 years ago)
Author:
ejucovy
Message:

refs #1541: by adding a function _getFirstAncestorAndWhy underneath _getFirstAncestor which returns slightly more information, we can simplify the logic for figuring out which formatblock option to display as selected in the dropdown.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/XinhaCore.js

    r1288 r1289  
    49094909        } 
    49104910 
    4911         var deepestAncestor = this._getFirstAncestor(this.getSelection(), blocks); 
     4911        var match = this._getFirstAncestorAndWhy(this.getSelection(), blocks); 
     4912        var deepestAncestor = match[0]; 
     4913        var matchIndex = match[1];  
     4914 
     4915 
    49124916        if ( deepestAncestor ) 
    49134917        { 
    4914           for ( var x = 0; x < blocks.length; x++ ) 
    4915           { 
    4916             if ( blocks[x].toLowerCase() == deepestAncestor.tagName.toLowerCase() ) 
    4917             { 
    4918               btn.element.selectedIndex = x; 
    4919             } 
    4920           } 
     4918          // the function can return null for its second element even if a match is found, 
     4919          // but we passed in an array, so we know it will be a numerical index. 
     4920          btn.element.selectedIndex = matchIndex; 
    49214921        } 
    49224922        else 
     
    50435043/** Traverses the DOM upwards and returns the first element that is of one of the specified types 
    50445044 *  @param {Selection} sel  Selection object as returned by getSelection 
    5045  *  @param {Array} types Array of matching criteria.  Each criteria is either a string containing the tag name, or a callback used to select the element. 
     5045 *  @param {Array|String} types Array of matching criteria.  Each criteria is either a string containing the tag name, or a callback used to select the element. 
    50465046 *  @returns {DomNode|null}  
    50475047 */ 
    50485048Xinha.prototype._getFirstAncestor = function(sel, types) 
     5049{ 
     5050  return this._getFirstAncestorAndWhy(sel, types)[0]; 
     5051}; 
     5052 
     5053/** Traverses the DOM upwards and returns the first element that is one of the specified types, 
     5054 *  and which (of the specified types) the found element successfully matched. 
     5055 *  @param {Selection} sel  Selection object as returned by getSelection 
     5056 *  @param {Array|String} types Array of matching criteria.  Each criteria is either a string containing the tag name, or a callback used to select the element. 
     5057 *  @returns {Array} The array will look like [{DomNode|null}, {Integer|null}] -- that is, it always contains two elements.  The first element is the element that matched, or null if no match was found. The second is the numerical index that can be used to identify which element of the "types" was responsible for the match.  It will be null if no match was found.  It will also be null if the "types" argument was omitted.  
     5058 */ 
     5059Xinha.prototype._getFirstAncestorAndWhy = function(sel, types) 
    50495060{ 
    50505061  var prnt = this.activeElement(sel); 
     
    50585069    catch(ex) 
    50595070    { 
    5060       return null; 
     5071      return [null, null]; 
    50615072    } 
    50625073  } 
     
    50735084      if ( types === null ) 
    50745085      { 
    5075         return prnt; 
     5086        return [prnt, null]; 
    50765087      } 
    50775088      for (var index=0; index<types.length; ++index) { 
    50785089        if (typeof types[index] == 'string' && types[index] == prnt.tagName.toLowerCase()){ 
    50795090          // Criteria is a tag name.  It matches 
    5080         return prnt; 
     5091          return [prnt, index]; 
    50815092      } 
    50825093        else if (typeof types[index] == 'function' && types[index](this, prnt)) { 
    50835094          // Criteria is a callback.  It matches 
    5084           return prnt; 
     5095          return [prnt, index]; 
    50855096        } 
    50865097      } 
     
    50985109  } 
    50995110 
    5100   return null; 
     5111  return [null, null]; 
    51015112}; 
    51025113 
Note: See TracChangeset for help on using the changeset viewer.