diff options
| author | Igor Minar | 2011-02-15 01:12:45 -0500 |
|---|---|---|
| committer | Igor Minar | 2011-02-15 11:01:53 -0500 |
| commit | 1777110958f76ee4be5760e36c96702223385918 (patch) | |
| tree | 5aa03b246507e66877e5eac69e58e004e244d7a5 /src/service/invalidWidgets.js | |
| parent | d2089a16335276eecb8d81eb17332c2dff2cf1a2 (diff) | |
| download | angular.js-1777110958f76ee4be5760e36c96702223385918.tar.bz2 | |
split up services into individual files
- split up services into files under src/service
- split up specs into files under test/service
- rewrite all specs so that they don't depend on one global forEach
- get rid of obsolete code and tests in ng:switch
- rename mock $log spec from "$log" to "$log mock"
Diffstat (limited to 'src/service/invalidWidgets.js')
| -rw-r--r-- | src/service/invalidWidgets.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/service/invalidWidgets.js b/src/service/invalidWidgets.js new file mode 100644 index 00000000..af31d61d --- /dev/null +++ b/src/service/invalidWidgets.js @@ -0,0 +1,67 @@ +/** + * @workInProgress + * @ngdoc service + * @name angular.service.$invalidWidgets + * + * @description + * Keeps references to all invalid widgets found during validation. + * Can be queried to find whether there are any invalid widgets currently displayed. + * + * @example + */ +angularServiceInject("$invalidWidgets", function(){ + var invalidWidgets = []; + + + /** Remove an element from the array of invalid widgets */ + invalidWidgets.markValid = function(element){ + var index = indexOf(invalidWidgets, element); + if (index != -1) + invalidWidgets.splice(index, 1); + }; + + + /** Add an element to the array of invalid widgets */ + invalidWidgets.markInvalid = function(element){ + var index = indexOf(invalidWidgets, element); + if (index === -1) + invalidWidgets.push(element); + }; + + + /** Return count of all invalid widgets that are currently visible */ + invalidWidgets.visible = function() { + var count = 0; + forEach(invalidWidgets, function(widget){ + count = count + (isVisible(widget) ? 1 : 0); + }); + return count; + }; + + + /* At the end of each eval removes all invalid widgets that are not part of the current DOM. */ + this.$onEval(PRIORITY_LAST, function() { + for(var i = 0; i < invalidWidgets.length;) { + var widget = invalidWidgets[i]; + if (isOrphan(widget[0])) { + invalidWidgets.splice(i, 1); + if (widget.dealoc) widget.dealoc(); + } else { + i++; + } + } + }); + + + /** + * Traverses DOM element's (widget's) parents and considers the element to be an orphant if one of + * it's parents isn't the current window.document. + */ + function isOrphan(widget) { + if (widget == window.document) return false; + var parent = widget.parentNode; + return !parent || isOrphan(parent); + } + + return invalidWidgets; +}, [], true); |
