|
| 1 | + |
| 2 | +// FIXME: type template (using custom builder) |
| 3 | +angular.module('schemaForm').factory('sfBuilder', |
| 4 | +['$templateCache', 'schemaFormDecorators', 'sfPath', function($templateCache, schemaFormDecorators, sfPath) { |
| 5 | + |
| 6 | + var SNAKE_CASE_REGEXP = /[A-Z]/g; |
| 7 | + var snakeCase = function(name, separator) { |
| 8 | + separator = separator || '_'; |
| 9 | + return name.replace(SNAKE_CASE_REGEXP, function(letter, pos) { |
| 10 | + return (pos ? separator : '') + letter.toLowerCase(); |
| 11 | + }); |
| 12 | + }; |
| 13 | + |
| 14 | + |
| 15 | + var checkForSlot = function(form, slots) { |
| 16 | + // Finally append this field to the frag. |
| 17 | + // Check for slots |
| 18 | + if (form.key) { |
| 19 | + var slot = slots[sfPath.stringify(form.key)]; |
| 20 | + console.log('checking for slots!', sfPath.stringify(form.key), slot) |
| 21 | + if (slot) { |
| 22 | + console.log('clearing and appending to slot') |
| 23 | + while (slot.firstChild) { |
| 24 | + slot.removeChild(slot.firstChild); |
| 25 | + } |
| 26 | + return slot; |
| 27 | + } |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + |
| 32 | + var build = function(items, decorator, templateFn, slots, path) { |
| 33 | + path = path || 'schemaForm.form'; |
| 34 | + var container = document.createDocumentFragment(); |
| 35 | + items.reduce(function(frag, f, index) { |
| 36 | + |
| 37 | + // Sanity check. |
| 38 | + if (!f.type) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + var field = decorator[f.type] || decorator['default']; |
| 43 | + if (!field.replace) { |
| 44 | + // Backwards compatability build |
| 45 | + var n = document.createElement(snakeCase(decorator.__name, '-')); |
| 46 | + n.setAttribute('form', path + '[' + index + ']'); |
| 47 | + (checkForSlot(f, slots) || frag).appendChild(n); |
| 48 | + |
| 49 | + } else { |
| 50 | + var tmpl; |
| 51 | + |
| 52 | + // TODO: Create a couple fo testcases, small and large and |
| 53 | + // measure optmization. A good start is probably a cache of DOM nodes for a particular |
| 54 | + // template that can be cloned instead of using innerHTML |
| 55 | + var div = document.createElement('div'); |
| 56 | + var template = templateFn(field.template) || templateFn([decorator['default'].template]); |
| 57 | + if (f.key) { |
| 58 | + var key = f.key ? |
| 59 | + sfPath.stringify(f.key).replace(/"/g, '"') : ''; |
| 60 | + template = template.replace( |
| 61 | + /\$\$value\$\$/g, |
| 62 | + 'model' + (key[0] !== '[' ? '.' : '') + key |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + div.innerHTML = template; |
| 67 | + tmpl = document.createDocumentFragment(); |
| 68 | + |
| 69 | + var len = div.childNodes.length; |
| 70 | + for (var i = 0; i < len; i++) { |
| 71 | + tmpl.appendChild(div.childNodes[i]); |
| 72 | + } |
| 73 | + |
| 74 | + tmpl.firstChild.setAttribute('sf-field',path + '[' + index + ']'); |
| 75 | + |
| 76 | + // Possible builder, often a noop |
| 77 | + field.builder({ |
| 78 | + fieldFrag: tmpl, |
| 79 | + form: f, |
| 80 | + path: path + '[' + index + ']', |
| 81 | + |
| 82 | + // Recursive build fn |
| 83 | + build: function(items, path) { |
| 84 | + return build(items, decorator, templateFn, slots, path); |
| 85 | + }, |
| 86 | + |
| 87 | + }); |
| 88 | + |
| 89 | + // Append |
| 90 | + (checkForSlot(f, slots) || frag).appendChild(tmpl); |
| 91 | + } |
| 92 | + return frag; |
| 93 | + }, container); |
| 94 | + |
| 95 | + return container; |
| 96 | + }; |
| 97 | + |
| 98 | + |
| 99 | +/* FIXME: make a utility function of this ordinary case |
| 100 | +var transclusion = function() { |
| 101 | + // We might be able to micro optimize here with some kind of setting |
| 102 | + // or by checking the schema for the type (when we have those.) |
| 103 | + // but a quick jsperf did 55 000 querySelectorAll per second (on my laptop), |
| 104 | + // so I think this isn't the main performance hog. |
| 105 | + var transclusions = tmpl.querySelectorAll('[sf-transclude]'); |
| 106 | +
|
| 107 | + if (transclusions.length) { |
| 108 | + // Before we do any transclusion we need clone the cache for later use, but just the first time. |
| 109 | + if ([f.type] === tmpl) { |
| 110 | + [f.type] = [f.type].cloneNode(true); |
| 111 | + } |
| 112 | +
|
| 113 | + for (var i = 0; i < transclusions.length; i++) { |
| 114 | + var n = transclusions[i]; |
| 115 | +
|
| 116 | + // The sf-transclude attribute is not a directive, but has the name of what we're supposed to |
| 117 | + // traverse. FIXME: Tabs? How do we loop over something that is not a list of forms? |
| 118 | + // maybe callback? |
| 119 | + var sub = form[n.getAttribute('sf-transclude')]; |
| 120 | + if (sub) { |
| 121 | + sub = Array.isArray(sub) ? sub : [sub]; |
| 122 | +
|
| 123 | + // Build the subform recursivly |
| 124 | + n.appendChild( build(sub, templates, ) ); |
| 125 | +
|
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +
|
| 130 | +}*/ |
| 131 | + |
| 132 | + |
| 133 | + var builder = { |
| 134 | + /** |
| 135 | + * Builds a form from a canonical form definition |
| 136 | + */ |
| 137 | + build: function(form, decorator, slots) { |
| 138 | +console.warn(slots) |
| 139 | + return build(form, decorator, function(url) { |
| 140 | + return $templateCache.get(url) || ''; |
| 141 | + }, slots); |
| 142 | + |
| 143 | + }, |
| 144 | + internalBuild: build |
| 145 | + }; |
| 146 | + return builder; |
| 147 | + |
| 148 | +}]); |
0 commit comments