1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1 1 1 1 1 14 14 14 10 1 9 9 2 9 9 9 9 9 9 9 9 19 19 19 15 4 4 4 3 1 4 4 10 14 2 2 2 2 2 2 2 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 14 10 10 1 6 6 6 6 6 14 14 14 14 14 14 14 14 14 14 15 14 5 14 53 14 14 6 1 1 14 | // FIXME: type template (using custom builder) angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(sfPathProvider) { var SNAKE_CASE_REGEXP = /[A-Z]/g; var snakeCase = function(name, separator) { separator = separator || '_'; return name.replace(SNAKE_CASE_REGEXP, function(letter, pos) { return (pos ? separator : '') + letter.toLowerCase(); }); }; var formId = 0; var builders = { sfField: function(args) { args.fieldFrag.firstChild.setAttribute('sf-field', formId); // We use a lookup table for easy access to our form. args.lookup['f' + formId] = args.form; formId++; }, ngModel: function(args) { if (!args.form.key) { return; } var key = args.form.key; // Redact part of the key, used in arrays // KISS keyRedaction is a number. if (args.state.keyRedaction) { key = key.slice(args.state.keyRedaction); } // Stringify key. var modelValue; Eif (!args.state.modelValue) { var strKey = sfPathProvider.stringify(key).replace(/"/g, '"'); modelValue = (args.state.modelName || 'model'); Eif (strKey) { // Sometimes, like with arrays directly in arrays strKey is nothing. modelValue += (strKey[0] !== '[' ? '.' : '') + strKey; } } else { // Another builder, i.e. array has overriden the modelValue modelValue = args.state.modelValue; } // Find all sf-field-value attributes. // No value means a add a ng-model. // sf-field-value="replaceAll", loop over attributes and replace $$value$$ in each. // sf-field-value="attrName", replace or set value of that attribute. var nodes = args.fieldFrag.querySelectorAll('[sf-field-model]'); for (var i = 0; i < nodes.length; i++) { var n = nodes[i]; var conf = n.getAttribute('sf-field-model'); if (!conf || conf === '') { n.setAttribute('ng-model', modelValue); } else Iif (conf === 'replaceAll') { var attributes = n.attributes; for (var j = 0; j < attributes.length; j++) { if (attributes[j].value && attributes[j].value.indexOf('$$value') !== -1) { attributes[j].value = attributes[j].value.replace(/\$\$value\$\$/g, modelValue); } } } else { var val = n.getAttribute(conf); if (val && val.indexOf('$$value$$')) { n.setAttribute(conf, val.replace(/\$\$value\$\$/g, modelValue)); } else { n.setAttribute(conf, modelValue); } } } }, simpleTransclusion: function(args) { var children = args.build(args.form.items, args.path + '.items', args.state); args.fieldFrag.firstChild.appendChild(children); }, // Patch on ngModelOptions, since it doesn't like waiting for its value. ngModelOptions: function(args) { Iif (args.form.ngModelOptions && Object.keys(args.form.ngModelOptions).length > 0) { args.fieldFrag.firstChild.setAttribute('ng-model-options', JSON.stringify(args.form.ngModelOptions)); } }, transclusion: function(args) { var transclusions = args.fieldFrag.querySelectorAll('[sf-field-transclude]'); if (transclusions.length) { for (var i = 0; i < transclusions.length; i++) { var n = transclusions[i]; // The sf-transclude attribute is not a directive, but has the name of what we're supposed to // traverse. var sub = args.form[n.getAttribute('sf-field-transclude')]; if (sub) { sub = Array.isArray(sub) ? sub : [sub]; var childFrag = args.build(sub, args.path + '.' + sub, args.state); n.appendChild(childFrag); } } } }, condition: function(args) { // Do we have a condition? Then we slap on an ng-if on all children, // but be nice to existing ng-if. if (args.form.condition) { var evalExpr = 'evalExpr(' + args.path + '.contidion, { model: model, "arrayIndex": $index})'; Eif (args.form.key) { var strKey = sfPathProvider.stringify(args.form.key); evalExpr = 'evalExpr(' + args.path + '.condition,{ model: model, "arrayIndex": $index, ' + '"modelValue": model' + (strKey[0] === '[' ? '' : '.') + strKey + '})'; } var children = args.fieldFrag.children || args.fieldFrag.childNodes; console.log('children', children, args.firstChild) for (var i = 0; i < children.length; i++) { var child = children[i]; var ngIf = child.getAttribute('ng-if'); child.setAttribute( 'ng-if', ngIf ? '(' + ngIf + ') || (' + evalExpr + ')' : evalExpr ); } } }, array: function(args) { var items = args.fieldFrag.querySelector('[schema-form-array-items]'); Eif (items) { state = angular.copy(args.state); state.keyRedaction = state.keyRedaction || 0; state.keyRedaction += args.form.key.length + 1; // Special case, an array with just one item in it that is not an object. // So then we just override the modelValue Iif (args.form.schema && args.form.schema.items && args.form.schema.items.type && args.form.schema.items.type.indexOf('object') === -1 && args.form.schema.items.type.indexOf('array') === -1) { var strKey = sfPathProvider.stringify(args.form.key).replace(/"/g, '"') + '[$index]'; state.modelValue = 'modelArray[$index]'; } else { state.modelName = 'item'; } // Flag to the builder that where in an array. // This is needed for compatabiliy if a "old" add-on is used that // hasn't been transitioned to the new builder. state.arrayCompatFlag = true; var childFrag = args.build(args.form.items, args.path + '.items', state); items.appendChild(childFrag); } } }; this.builders = builders; this.$get = ['$templateCache', 'schemaFormDecorators', 'sfPath', function($templateCache, schemaFormDecorators, sfPath) { var checkForSlot = function(form, slots) { // Finally append this field to the frag. // Check for slots if (form.key) { var slot = slots[sfPath.stringify(form.key)]; Iif (slot) { while (slot.firstChild) { slot.removeChild(slot.firstChild); } return slot; } } }; var build = function(items, decorator, templateFn, slots, path, state, lookup) { state = state || {}; lookup = lookup || Object.create(null); path = path || 'schemaForm.form'; var container = document.createDocumentFragment(); items.reduce(function(frag, f, index) { // Sanity check. Iif (!f.type) { return; } var field = decorator[f.type] || decorator['default']; Iif (!field.replace) { // Backwards compatability build var n = document.createElement(snakeCase(decorator.__name, '-')); if (state.arrayCompatFlag) { n.setAttribute('form','copyWithIndex($index)'); } else { n.setAttribute('form', path + '[' + index + ']'); } (checkForSlot(f, slots) || frag).appendChild(n); } else { var tmpl; // Reset arrayCompatFlag, it's only valid for direct children of the array. state.arrayCompatFlag = false; // TODO: Create a couple fo testcases, small and large and // measure optmization. A good start is probably a cache of DOM nodes for a particular // template that can be cloned instead of using innerHTML var div = document.createElement('div'); var template = templateFn(field.template) || templateFn([decorator['default'].template]); div.innerHTML = template; // Move node to a document fragment, we don't want the div. tmpl = document.createDocumentFragment(); while (div.childNodes.length > 0) { tmpl.appendChild(div.childNodes[0]); } // Possible builder, often a noop var args = { fieldFrag: tmpl, form: f, lookup: lookup, state: state, path: path + '[' + index + ']', // Recursive build fn build: function(items, path, state) { return build(items, decorator, templateFn, slots, path, state, lookup); }, }; // Builders are either a function or a list of functions. Iif (typeof field.builder === 'function') { field.builder(args); } else { field.builder.forEach(function(fn) { fn(args); }); } // Append (checkForSlot(f, slots) || frag).appendChild(tmpl); } return frag; }, container); return container; }; return { /** * Builds a form from a canonical form definition */ build: function(form, decorator, slots, lookup) { return build(form, decorator, function(url) { return $templateCache.get(url); }, slots, undefined, undefined, lookup); }, builder: builders, internalBuild: build }; }]; }]); |