-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ba3980
commit 7e35b34
Showing
2 changed files
with
4,129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/*can-view-autorender@4.0.0-pre.0#can-view-autorender*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-view-model', | ||
'can-util/js/string', | ||
'can-util/js/each', | ||
'can-util/js/import', | ||
'can-namespace', | ||
'can-util/dom/events' | ||
], 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 namespace = require('can-namespace'); | ||
var domEvents = require('can-util/dom/events'); | ||
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); | ||
}); | ||
domEvents.addEventListener.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 { | ||
domEvents.addEventListener.call(window, 'load', autoload); | ||
} | ||
}); | ||
module.exports = namespace.autorender = function autorender(success, error) { | ||
return promise.then(success, error); | ||
}; | ||
}); |
Oops, something went wrong.