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

## Statements: 93.62% (44 / 47) Branches: 88.24% (30 / 34) Functions: 100% (6 / 6) Lines: 93.48% (43 / 46) Ignored: none

All files » lib/template/ » 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
    1641

    1

    1

    1

    308
    308
    308
    308
    308
    308
    308
    308

    308
    169

    139

    308

    308

    1

    308

    308

    308

    308
    111
    111

    197

    308
    225

    7

    139

    267

    267

    267

    267
    267
    267

    1

    170

    170
    166
    4
    3

    1

    169
     var protoclass    = require("protoclass"),
    nofactor          = require("nofactor"),
    parser            = require("../parser"),
    BlockNode         = require("./vnode/block"),
    ElementNode       = require("./vnode/element"),
    FragmentNode      = require("./vnode/fragment"),
    TextNode          = require("./vnode/text"),
    CommentNode       = require("./vnode/comment"),
    View              = require("./view"),
    FragmentSection   = require("../section/fragment"),
    NodeSection       = require("../section/node"),
    TemplateComponent = require("./component"),
    defaults          = require("../defaults"),
    defaultRunner     = require("../defaultRunner"),
    DefaultScope      = require("../scope/bindableObject");

    /**
     * Compiles the template
     */

    var isIE = false;

    // check for all versions of IE - IE doesn't properly support
    // element.cloneNode(true), so we can't use that optimization.
    Iif (process.browser) {
      isIE = !!(~navigator.userAgent.toLowerCase().indexOf("msie") || ~navigator.userAgent.toLowerCase().indexOf("trident"))
    }

    function Template (script, options) {

      this.options         = options;
      this.scopeClass      = options.scopeClass || DefaultScope;
      this.components      = options.components  || {};
      this.modifiers       = options.modifiers   || {};
      this.attributes      = options.attributes  || {};
      this.runner          = options.runner      || defaultRunner;
      this.useCloneNode    = !isIE;
      this.nodeFactory     = options.nodeFactory || nofactor.default;

      if (typeof script === "function") {
        this.vnode = script(
          FragmentNode.create,
          BlockNode.create,
          ElementNode.create,
          TextNode.create,
          CommentNode.create,
          void 0,
          this.modifiers
        )
      } else {
        this.vnode = script;
      }

      this._viewPool   = [];

      this.initialize();
    }

    module.exports = protoclass(Template, {

      /**
       */

      initialize: function () {
        this.hydrators = [];

        // first build the cloneable DOM node
        this.section = new FragmentSection(this.nodeFactory);

        var node = this.vnode.initialize(this);

        if (node.nodeType === 11) {
          this.section = new FragmentSection(this.nodeFactory);
          this.section.appendChild(node);
        } else {
          this.section = new NodeSection(this.nodeFactory, node);
        }

        // next we need to initialize the hydrators - many of them
        // keep track of the path to a particular nodes.
        for (var i = this.hydrators.length; i--;) {
          this.hydrators[i].initialize();
        }
      },

      /**
       */

      createComponentClass: function (contextClass) {
        return TemplateComponent.extend({
          template     : this,
          contextClass : contextClass || Object
        });
      },

      /**
       * Creates a child template with the same options, difference source.
       * This method allows child nodes to have a different context, or the same
       * context of a different template. Used in components.
       */

      child: function (vnode) {
        return new Template(vnode, this.options);
      },

      /**
       * Creates a new or recycled view which binds a cloned node
       * from the template to a context (or view scope).
       */

      view: function (scope) {

        var clonedSection;

        /*
          TODO (for IE):

          if (internetExplorer) {
            var clone = this.nodeCreator.createNode();
          } else {
            if (!this._templateNode) this._templateNode = this.nodeCreator.createNode();
            var clone = this._templateNode.clone();
          }
         */

        // re-init for now
        Iif (!this.useCloneNode) {
          this.initialize();
          clonedSection = this.section;
        } else {
          clonedSection = this.section.clone();
        }

        var view = this._viewPool.pop() || new View(this, this._viewPool, clonedSection, this.hydrators);
        if (scope) view.bind(scope);
        return view;
      }
    });

    /**
     */

    module.exports = function (source, options) {

      var script, tos = typeof source, options;

      if (tos === "string") {
        script = parser.compile(source);
      } else if (tos === "function") {
        script = source;
      } else {
        throw new Error("source must either be type 'string' or 'function'");
      }

      /**
       * Note: the template used to be cached on the script, but this isn't
       * possible now since components are registered on the template level.
       */

      return new Template(script, options || defaults);
    }