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

## Statements: 100% (37 / 37) Branches: 100% (24 / 24) Functions: 100% (5 / 5) Lines: 100% (36 / 36) 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
    1351

    1

    304
    304
    304
    304
    304
    304

    304
    166

    138

    304

    304

    304

    1

    304

    304

    304

    304
    110
    110

    194

    304
    220

    138

    260
    260
    260

    1

    167

    167
    163
    4
    3

    1

    166
     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");

    /**
     * Compiles the template
     */

    function Template (script, options) {

      this.options     = options;
      this.components  = options.components  || {};
      this.modifiers   = options.modifiers   || {};
      this.attributes  = options.attributes  || {};
      this.runner      = options.runner      || defaultRunner;
      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.component = TemplateComponent.extend({ template: this });

      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();
        }
      },

      /**
       * 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 controller).
       */

      view: function (context) {

        /*
          TODO (for IE):

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

        var view = this._viewPool.pop() || new View(this, this._viewPool, this.section.clone(), this.hydrators);
        if (context) view.bind(context);
        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);
    }