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

## Statements: 100% (57 / 57) Branches: 91.3% (21 / 23) Functions: 100% (5 / 5) Lines: 100% (56 / 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
    270
    3

    1
    159
    159
    159

    1

    202

    202

    99

    99

    99

    103
    103
    103
    103
    103

    103

    111
    111
    111
    111
    111

    111

    111

    4
    4

    4

    107
    54

    53

    44

    9

    103
    44
    44
    43

    103

    103

    1

    159

    159
    174
    174

    159

     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?
        if (componentClass) {

          // create a dynamic section - this is owned by the component
          var section = new FragmentSection(template.nodeFactory);

          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.nodeFactory.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;

          if (attrComponentClass) {

            // TODO - element might need to be a sub view
            Eif (!elementSection) {
              elementSection = new NodeSection(template.nodeFactory, 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];
          if (typeof v === "string") {
            element.setAttribute(k, vanillaAttrs[k]);
          }
        }

        // no component class with the attrs? append the children
        if (!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));
    };