|
| 1 | +// angular-tree-repeat - v0.0.2 |
| 2 | + |
| 3 | +// Include this first to define the module that the directives etc. hang off. |
| 4 | +// |
| 5 | +(function(){ |
| 6 | +'use strict'; |
| 7 | +angular.module('sf.treeRepeat', []); |
| 8 | +}()); |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +// sf-tree-repeat directive |
| 13 | +// ======================== |
| 14 | +// Like `ng-repeat` but recursive |
| 15 | +// |
| 16 | +(function(){ |
| 17 | + 'use strict'; |
| 18 | + // (part of the sf.treeRepeat module). |
| 19 | + var mod = angular.module('sf.treeRepeat'); |
| 20 | + |
| 21 | + // Utility to turn the expression supplied to the directive: |
| 22 | + // |
| 23 | + // a in b of c |
| 24 | + // |
| 25 | + // into `{ value: "a", collection: "b", root: "c" }` |
| 26 | + // |
| 27 | + function parseRepeatExpression(expression){ |
| 28 | + var match = expression.match(/^\s*([\$\w]+)\s+in\s+([\S\s]*)\s+of\s+([\S\s]*)$/); |
| 29 | + if (! match) { |
| 30 | + throw new Error("Expected sfTreepeat in form of"+ |
| 31 | + " '_item_ in _collection_ of _root_' but got '" + |
| 32 | + expression + "'."); |
| 33 | + } |
| 34 | + return { |
| 35 | + value: match[1], |
| 36 | + collection: match[2], |
| 37 | + root: match[3] |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + // The `sf-treepeat` directive is the main and outer directive. Use this to |
| 42 | + // define your tree structure in the form `varName in collection of root` |
| 43 | + // where: |
| 44 | + // - varName is the scope variable used for each node in the tree. |
| 45 | + // - collection is the collection of children within each node. |
| 46 | + // - root is the root node. |
| 47 | + // |
| 48 | + mod.directive('sfTreepeat', ['$log', function($log) { |
| 49 | + return { |
| 50 | + restrict: 'A', |
| 51 | + // Use a scope to attach the node model |
| 52 | + scope: true, |
| 53 | + // and a controller to communicate the template params to `sf-treecurse` |
| 54 | + controller: ['$scope', '$attrs', |
| 55 | + function TreepeatController($scope, $attrs){ |
| 56 | + var ident = this.ident = parseRepeatExpression($attrs.sfTreepeat); |
| 57 | + if($attrs.class) { |
| 58 | + this.ident.class = $attrs.class; |
| 59 | + ident = this.ident; |
| 60 | + } |
| 61 | + $log.info("Parsed '%s' as %s", $attrs.sfTreepeat, JSON.stringify(this.ident)); |
| 62 | + // Keep the root node up to date. |
| 63 | + $scope.$watch(this.ident.root, function(v){ |
| 64 | + $scope[ident.value] = v; |
| 65 | + }); |
| 66 | + } |
| 67 | + ], |
| 68 | + // Get the original element content HTML to use as the recursive template |
| 69 | + compile: function sfTreecurseCompile(element){ |
| 70 | + var template = element.html(); |
| 71 | + return { |
| 72 | + // set it in the pre-link so we can use it lower down |
| 73 | + pre: function sfTreepeatPreLink(scope, iterStartElement, attrs, controller){ |
| 74 | + controller.template = template; |
| 75 | + } |
| 76 | + }; |
| 77 | + } |
| 78 | + }; |
| 79 | + }]); |
| 80 | + |
| 81 | + // The `sf-treecurse` directive is a little like `ng-transclude` in that it |
| 82 | + // signals where to insert our recursive template |
| 83 | + mod.directive('sfTreecurse', ['$compile', function($compile){ |
| 84 | + return { |
| 85 | + // which must come from a parent `sf-treepeat`. |
| 86 | + require: "^sfTreepeat", |
| 87 | + link: function sfTreecursePostLink(scope, iterStartElementJqLiteOrJquery, attrs, controller) { |
| 88 | + // Now we stitch together an element containing a vanila repeater using |
| 89 | + // the values from the controller. |
| 90 | + var iterStartElementDOM = iterStartElementJqLiteOrJquery[0]; |
| 91 | + var build = [ |
| 92 | + '<', iterStartElementDOM.tagName, ' ng-repeat="', |
| 93 | + controller.ident.value, ' in ', |
| 94 | + controller.ident.value, '.', controller.ident.collection, |
| 95 | + '" class="', controller.ident.class, |
| 96 | + '">', |
| 97 | + controller.template, |
| 98 | + '</', iterStartElementDOM.tagName, '>']; |
| 99 | + var el = angular.element(build.join('')); |
| 100 | + // We swap out the element for our new one and tell angular to do its |
| 101 | + // thing with that. |
| 102 | + iterStartElementJqLiteOrJquery.replaceWith(el); |
| 103 | + $compile(el)(scope); |
| 104 | + } |
| 105 | + }; |
| 106 | + }]); |
| 107 | + |
| 108 | +}()); |
0 commit comments