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

## Statements: 100% (39 / 39) Branches: 90.48% (19 / 21) Functions: 100% (3 / 3) Lines: 100% (38 / 38) 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
    1691

    1
    118
    118
    118

    1

    161

    161

    90

    90

    90

    71

    71

    71

    71
    71

    71

    71

    3
    3

    3

    68
    31

    37

    29

    8

    71
    29

    71

    71

    1

    118

    118
    119
    119
    81

    38

    118
     var protoclass        = require("protoclass"),
    FragmentSection       = require("../../../section/fragment"),
    NodeSection           = require("../../../section/node"),
    Fragment              = require("../fragment"),
    utils                 = require("../../../utils"),
    BindableObject        = require("bindable-object"),
    script                = require("../../../script"),
    ComponentHydrator     = require("./componentHydrator"),
    AttributeHydrator     = require("./attributeHydrator"),
    ValueAttribute        = require("./valueAttribute");

    /**
     */

    function Element (name, attributes, children) {
      this.name       = 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),
        hasAttrComponent = false,
        vanillaAttrs = {};

        var elementSection;

        // components should be attachable to regular DOM elements as well
        for (var k in this.attributes) {

          var v = this.attributes[k], tov = typeof v;
          var attrComponentClass = template.components[k],
          attrClass = template.attributes[k];
          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 === "string") {
              // 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 (var k in vanillaAttrs) {
          element.setAttribute(k, vanillaAttrs[k]);
        }

        // no component class with the attrs? append the children
        if (!hasAttrComponent) element.appendChild(this.children.initialize(template));

        return element;
      }
    });

    /*

    element("video", [block()])
    */

    module.exports.create = function (name, attributes, children) {

      var setter = new BindableObject({a:{}});

      // check the attributes for any scripts - pluck them out
      // TODO - check for attribute components - apply the same
      // logic as components

      // 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];
        if (typeof v === "object") {
          setter.set("a." + k, script(v));
        } else {
          setter.set("a." + k, v);
        }
      }

      // TODO - check for registered components,
      return new Element(name, setter.a, new Fragment(children));
    }