Skip to content

Commit

Permalink
Factor into functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Oct 14, 2015
1 parent 758c483 commit b2117dc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
17 changes: 11 additions & 6 deletions src/lib/annotations/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,23 @@
}
},

_literalFromParts: function(parts) {
var s = '';
for (var i=0; i<parts.length; i++) {
var literal = parts[i].literal;
s += literal || '';
}
return s;
},

// add annotations gleaned from TextNode `node` to `list`
_parseTextNodeAnnotation: function(node, list) {
var parts = this._parseBindings(node.textContent);
if (parts) {
// Initialize the textContent with any literal parts
// NOTE: default to a space here so the textNode remains; some browsers
// (IE) evacipate an empty textNode following cloneNode/importNode.
node.textContent = parts.map(function(part) {
return part.literal;
}).join('') || ' ';
node.textContent = this._literalFromParts(parts) || ' ';
var annote = {
bindings: [{
kind: 'text',
Expand Down Expand Up @@ -295,9 +302,7 @@
kind = 'attribute';
}
// Initialize attribute bindings with any literal parts
var literal = parts.map(function(part) {
return part.literal;
}).join('');
var literal = this._literalFromParts(parts);
if (kind == 'attribute') {
node.setAttribute(name, literal);
}
Expand Down
84 changes: 46 additions & 38 deletions src/standard/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,44 +225,52 @@
for (var i=0; i<notes.length; i++) {
var note = notes[i];
var node = nodes[i];
// nested template contents have been stored prototypically to avoid
// unnecessary duplication, here we put references to the
// indirected contents onto the nested template instances
if (note.templateContent) {
// note: we can rely on _nodes being set here and having the same
// index as _notes
this._nodes[i]._content = note.templateContent;
}
// Compound bindings utilize private storage on the node to store
// the current state of each value that will be concatenated to generate
// the final property/attribute/text value
// Here we initialize the private storage array on the node with any
// literal parts that won't change (could get fancy and use WeakMap),
// and configure property bindings to children with the literal parts
// (textContent and annotations were already initialized in the template)
var bindings = note.bindings;
for (var j=0; j<bindings.length; j++) {
var binding = bindings[j];
if (binding.isCompound) {
// Create compound storage map
var storage = node.__compoundStorage__ ||
(node.__compoundStorage__ = {});
var parts = binding.parts;
// Copy literals from parts into storage for this binding
var literals = new Array(parts.length);
for (var k=0; k<parts.length; k++) {
literals[k] = parts[k].literal;
}
var name = binding.name;
storage[name] = literals;
// Configure properties with their literal parts
if (binding.kind == 'property') {
var literal = literals.join('');
if (node._configValue) {
node._configValue(name, literal);
} else {
node[name] = literal;
}
this._configureTemplateContent(note, node);
this._configureCompoundBindings(note, node);
}
},

// nested template contents have been stored prototypically to avoid
// unnecessary duplication, here we put references to the
// indirected contents onto the nested template instances
_configureTemplateContent: function(note, node) {
if (note.templateContent) {
// note: we can rely on _nodes being set here and having the same
// index as _notes
node._content = note.templateContent;
}
},

// Compound bindings utilize private storage on the node to store
// the current state of each value that will be concatenated to generate
// the final property/attribute/text value
// Here we initialize the private storage array on the node with any
// literal parts that won't change (could get fancy and use WeakMap),
// and configure property bindings to children with the literal parts
// (textContent and annotations were already initialized in the template)
_configureCompoundBindings: function(note, node) {
var bindings = note.bindings;
for (var i=0; i<bindings.length; i++) {
var binding = bindings[i];
if (binding.isCompound) {
// Create compound storage map
var storage = node.__compoundStorage__ ||
(node.__compoundStorage__ = {});
var parts = binding.parts;
// Copy literals from parts into storage for this binding
var literals = new Array(parts.length);
for (var j=0; j<parts.length; j++) {
literals[j] = parts[j].literal;
}
var name = binding.name;
storage[name] = literals;
// Configure properties with their literal parts
if (binding.kind == 'property') {
var literal = literals.join('');
if (node._configValue) {
node._configValue(name, literal);
} else {
node[name] = literal;
}
}
}
Expand Down

0 comments on commit b2117dc

Please sign in to comment.