Code coverage report for lib/template/vnode/element/index.js

Statements: 89.47% (51 / 57)      Branches: 65.22% (15 / 23)      Functions: 100% (5 / 5)      Lines: 89.29% (50 / 56)      Ignored: none     

All files » lib/template/vnode/element/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1731 1 1 1 1 1 1 1 1 1         1 26 1             1 10 10 10           1             10     10                                                   10 10 10 10 10     10   16 16 16 16 16   16   16                                 16 3               13   7   6                                                 10 7 7 7         10   10       1           10       10 16 16       10    
var protoclass        = require("protoclass");
var FragmentSection   = require("../../../section/fragment");
var NodeSection       = require("../../../section/node");
var Fragment          = require("../fragment");
var utils             = require("../../../utils");
var script            = require("../../../script");
var ComponentHydrator = require("./componentHydrator");
var AttributeHydrator = require("./attributeHydrator");
var ValueAttribute    = require("./valueAttribute");
var _set              = require("../../../utils/set");
 
/**
 */
 
function _replaceDashes(k) {
  return k.replace(/\-./, function(k) {
    return k.substr(1).toUpperCase();
  });
}
 
/**
 */
 
function Element(name, attributes, children) {
  this.name       = _replaceDashes(name);
  this.attributes = attributes;
  this.children   = children;
}
 
/**
 */
 
module.exports = protoclass(Element, {
 
  /**
   */
 
  initialize: function(template) {
 
    var componentClass = template.components[this.name];
 
    // is a component present?
    Iif (componentClass) {
 
      // create a dynamic section - this is owned by the component
      var section = new FragmentSection(template.document);
 
      template.hydrators.push(new ComponentHydrator(
        this.name,
        this.attributes,
        template.child(this.children),
        section,
        componentClass
      ));
 
      // TODO:
 
      /*
        return {
          createNode: function() {
            return section.render();
          }
        }
      */
 
      return section.render();
    }
 
    var element          = template.document.createElement(this.name);
    var hasAttrComponent = false;
    var vanillaAttrs     = {};
    var elementSection;
    var v;
 
    // components should be attachable to regular DOM elements as well
    for (var k in this.attributes) {
 
      var k2                 = _replaceDashes(k);
      v                      = this.attributes[k];
      var tov                = typeof v;
      var attrComponentClass = template.components[k2];
      var attrClass          = template.attributes[k2];
 
      hasAttrComponent = !!attrComponentClass || hasAttrComponent;
 
      Iif (attrComponentClass) {
 
        // TODO - element might need to be a sub view
        if (!elementSection) {
          elementSection = new NodeSection(template.document, element);
        }
 
        template.hydrators.push(new ComponentHydrator(
          this.name,
 
          // v could be formatted as repeat.each, repeat.as. Need to check for this
          typeof v === "object" ? v : this.attributes,
          template.child(this.children),
          elementSection,
          attrComponentClass
        ));
 
      } else if (attrClass && (!attrClass.test || attrClass.test(v))) {
        template.hydrators.push(new AttributeHydrator(
          attrClass,
          k,
          v,
          element
        ));
      } else {
 
        if (tov !== "object") {
          // element.setAttribute(k, v);
          vanillaAttrs[k] = v;
        } else {
          template.hydrators.push(new AttributeHydrator(
            ValueAttribute,
            k,
            v,
            element
          ));
        }
      }
    }
 
    /*
      TODO: throw node creation in another object
 
      return {
        createNode: function() {
 
          var element = document.createElement()
          // no component class with the attrs? append the children
          if (!hasAttrComponent) element.appendChild(this.children.initialize(template));
 
          return element;
        }
      }
    */
 
    for (k in vanillaAttrs) {
      v = vanillaAttrs[k];
      Eif (typeof v !== "object") {
        element.setAttribute(k, vanillaAttrs[k]);
      }
    }
 
    // no component class with the attrs? append the children
    Eif (!hasAttrComponent) element.appendChild(this.children.initialize(template));
 
    return element;
  }
});
 
module.exports.create = function(name, attributes, children) {
 
  // check the attributes for any scripts - pluck them out
  // TODO - check for attribute components - apply the same
  // logic as components
 
  var attrs = {};
 
  // NOTE - a bit sloppy here, but we're hijacking the bindable object
  // setter functionality so we can properly get attrs for stuff like repeat.each
  for (var k in attributes) {
    var v = attributes[k];
    _set(attrs, k.toLowerCase(), typeof v === "object" ? script(v) : v);
  }
 
  // TODO - check for registered components,
  return new Element(name, attrs, new Fragment(children));
};