toolkit/javascript/d3/src/core/selection-attr.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 d3_selectionPrototype.attr = function(name, value) {
       
     2   name = d3.ns.qualify(name);
       
     3 
       
     4   // If no value is specified, return the first value.
       
     5   if (arguments.length < 2) {
       
     6     var node = this.node();
       
     7     return name.local
       
     8         ? node.getAttributeNS(name.space, name.local)
       
     9         : node.getAttribute(name);
       
    10   }
       
    11 
       
    12   function attrNull() {
       
    13     this.removeAttribute(name);
       
    14   }
       
    15 
       
    16   function attrNullNS() {
       
    17     this.removeAttributeNS(name.space, name.local);
       
    18   }
       
    19 
       
    20   function attrConstant() {
       
    21     this.setAttribute(name, value);
       
    22   }
       
    23 
       
    24   function attrConstantNS() {
       
    25     this.setAttributeNS(name.space, name.local, value);
       
    26   }
       
    27 
       
    28   function attrFunction() {
       
    29     var x = value.apply(this, arguments);
       
    30     if (x == null) this.removeAttribute(name);
       
    31     else this.setAttribute(name, x);
       
    32   }
       
    33 
       
    34   function attrFunctionNS() {
       
    35     var x = value.apply(this, arguments);
       
    36     if (x == null) this.removeAttributeNS(name.space, name.local);
       
    37     else this.setAttributeNS(name.space, name.local, x);
       
    38   }
       
    39 
       
    40   return this.each(value == null
       
    41       ? (name.local ? attrNullNS : attrNull) : (typeof value === "function"
       
    42       ? (name.local ? attrFunctionNS : attrFunction)
       
    43       : (name.local ? attrConstantNS : attrConstant)));
       
    44 };