|
| 1 | + |
| 2 | +// Sparky ready |
| 3 | +// |
| 4 | +// If jQuery is present and when the DOM is ready, traverse it looking for |
| 5 | +// data-model and data-ctrl attributes and use them to instantiate Sparky. |
| 6 | + |
| 7 | +(function(jQuery, Sparky) { |
| 8 | + if (!jQuery) { return; } |
| 9 | + |
| 10 | + var doc = jQuery(document); |
| 11 | + |
| 12 | + function isInTemplate(node) { |
| 13 | + if (node.tagName.toLowerCase() === 'template') { return true; } |
| 14 | + if (node === document.documentElement) { return false; } |
| 15 | + return isInTemplate(node.parentNode); |
| 16 | + } |
| 17 | + |
| 18 | + doc.ready(function(){ |
| 19 | + var start; |
| 20 | + |
| 21 | + if (window.console) { start = Date.now(); } |
| 22 | + |
| 23 | + var nodes = document.querySelectorAll('[data-ctrl], [data-model]'); |
| 24 | + var n = -1; |
| 25 | + var l = nodes.length; |
| 26 | + var node; |
| 27 | + var array = []; |
| 28 | + var modelPath; |
| 29 | + |
| 30 | + // Remove child sparkies |
| 31 | + while (++n < l) { |
| 32 | + node = nodes[n]; |
| 33 | + array.push(node); |
| 34 | + while (++n < l && node.contains(nodes[n])) { |
| 35 | + // But do add children that have absolute model paths. |
| 36 | + |
| 37 | + modelPath = nodes[n].getAttribute('data-model'); |
| 38 | + |
| 39 | + if (modelPath !== undefined && !/\{\{/.test(modelPath)) { |
| 40 | + //array.push(nodes[n]); |
| 41 | + } |
| 42 | + }; |
| 43 | + --n; |
| 44 | + } |
| 45 | + |
| 46 | + // Normally <template>s are inert, but if they are not a supported |
| 47 | + // feature their content is part of the DOM so we have to remove those, |
| 48 | + // too. |
| 49 | + if (!Sparky.features.template) { |
| 50 | + n = array.length; |
| 51 | + |
| 52 | + while (n--) { |
| 53 | + if (isInTemplate(array[n])) { |
| 54 | + array.splice(n, 1); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (Sparky.debug) { console.log('[Sparky] DOM nodes to initialise:', array); } |
| 60 | + |
| 61 | + array.forEach(function(node) { |
| 62 | + Sparky(node); |
| 63 | + }); |
| 64 | + |
| 65 | + window.requestAnimationFrame(function sparkyready() { |
| 66 | + doc.trigger('sparkyready'); |
| 67 | + }); |
| 68 | + |
| 69 | + if (window.console) { console.log('[Sparky] DOM initialised in ' + (Date.now() - start) + 'ms'); } |
| 70 | + }); |
| 71 | +})(jQuery, Sparky); |
0 commit comments