{"size": 1222, "ext": "js", "max_stars_repo_path": "watcher-count.js", "max_stars_repo_name": "Artimon/angular-watcher-count", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "watcher-count.js", "max_issues_repo_name": "Artimon/angular-watcher-count", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "watcher-count.js", "max_forks_repo_name": "Artimon/angular-watcher-count", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.4583333333, "max_line_length": 84, "alphanum_fraction": 0.624386252}
/* global angulr */

(function(angular) {

	'use strict';

	angular.module('pads.watcherCount', []).service('padsWatcherCount', function () {
		/**
		 * @param {HTMLElement} element
		 * @param {[]} [watchers]
		 * @returns {[]}
		 */
		function findWatchers(element, watchers) {
			watchers = watchers || [];

			angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) {
				if (
					element.data() &&
					element.data().hasOwnProperty(scopeProperty)
				) {
					angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) {
						watchers.push(watcher);
					});
				}
			});

			angular.forEach(element.children(), function (childElement) {
				findWatchers(angular.element(childElement), watchers);
			});

			return watchers;
		}

		return function getWatcherCount() {
			var root = angular.element(document.getElementsByTagName('body')),
				watchers = findWatchers(root),
				watchersWithoutDuplicates = [];

			angular.forEach(watchers, function (item) {
				if (watchersWithoutDuplicates.indexOf(item) < 0) {
					watchersWithoutDuplicates.push(item);
				}
			});

			return watchersWithoutDuplicates.length;
		};
	});
})(angular);