Skip to content

Commit

Permalink
3.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipskevin committed Aug 7, 2017
1 parent 9f40a38 commit 57e3a7d
Show file tree
Hide file tree
Showing 2 changed files with 3,680 additions and 0 deletions.
98 changes: 98 additions & 0 deletions dist/amd/can-view-autorender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*can-view-autorender@3.1.0#can-view-autorender*/
define([
'require',
'exports',
'module',
'can-view-model',
'can-util/js/string',
'can-util/js/each',
'can-util/js/import',
'can-event',
'can-util/namespace'
], function (require, exports, module) {
var canViewModel = require('can-view-model');
var camelize = require('can-util/js/string').camelize;
var each = require('can-util/js/each');
var importer = require('can-util/js/import');
var events = require('can-event');
var namespace = require('can-util/namespace');
var ignoreAttributesRegExp = /^(dataViewId|class|id|type|src)$/i;
var typeMatch = /\s*text\/(stache)\s*/;
function isIn(element, type) {
while (element.parentNode) {
element = element.parentNode;
if (element.nodeName.toLowerCase() === type.toLowerCase()) {
return true;
}
}
}
function setAttr(el, attr, scope) {
var camelized = camelize(attr);
if (!ignoreAttributesRegExp.test(camelized)) {
var value = el.getAttribute(attr);
if (scope.attr) {
scope.attr(camelized, value);
} else if (scope.set) {
scope.set(camelized, value);
} else {
scope[camelized] = value;
}
}
}
function insertAfter(ref, element) {
if (ref.nextSibling) {
ref.parentNode.insertBefore(element, ref.nextSibling);
} else {
ref.parentNode.appendChild(element);
}
}
function render(renderer, scope, el) {
var frag = renderer(scope);
if (isIn(el, 'head')) {
document.body.appendChild(frag);
} else if (el.nodeName.toLowerCase() === 'script') {
insertAfter(el, frag);
} else {
insertAfter(el, frag);
el.parentNode.removeChild(el);
}
}
function setupScope(el) {
var scope = canViewModel(el);
each(el.attributes || [], function (attr) {
setAttr(el, attr.name, scope);
});
events.on.call(el, 'attributes', function (ev) {
setAttr(el, ev.attributeName, scope);
});
return scope;
}
var promise = new Promise(function (resolve, reject) {
function autoload() {
var promises = [];
each(document.querySelectorAll('[can-autorender]'), function (el, i) {
el.style.display = 'none';
var text = el.innerHTML || el.text, typeAttr = el.getAttribute('type'), typeInfo = typeAttr.match(typeMatch), type = typeInfo && typeInfo[1], typeModule = 'can-' + type;
promises.push(importer(typeModule).then(function (engine) {
if (engine.async) {
return engine.async(text).then(function (renderer) {
render(renderer, setupScope(el), el);
});
} else {
var renderer = engine(text);
render(renderer, setupScope(el), el);
}
}));
});
Promise.all(promises).then(resolve, reject);
}
if (document.readyState === 'complete') {
autoload();
} else {
events.on.call(window, 'load', autoload);
}
});
module.exports = namespace.autorender = function autorender(success, error) {
return promise.then(success, error);
};
});
Loading

0 comments on commit 57e3a7d

Please sign in to comment.