1 | (function () {
|
---|
2 | angular.module('mobileNav')
|
---|
3 | .controller('NavCtrl', ['$scope', '$http', 'NavigationService', function ($scope, $http, navigationService) {
|
---|
4 |
|
---|
5 | $scope.navigation = navigationService;
|
---|
6 | $scope.fullTree = [];
|
---|
7 | $scope.flattenedTree = [];
|
---|
8 | $scope.search = "";
|
---|
9 |
|
---|
10 | // Check local storage first
|
---|
11 | if (navigationService.navInStorage()) {
|
---|
12 | $scope.fullTree = navigationService.getNavFromStorage();
|
---|
13 | // console.info("Mobile navigation pulled from " + navigationService.storageMedium + ".");
|
---|
14 | } else {
|
---|
15 | // Call the async method and then do stuff with what is returned inside the function
|
---|
16 | navigationService.getNavAsync()
|
---|
17 | .then(function (asyncData) {
|
---|
18 | // console.info("Mobile navigation pulled asyncronously.");
|
---|
19 | $scope.fullTree = asyncData;
|
---|
20 | });
|
---|
21 | }
|
---|
22 |
|
---|
23 | // Watch the fullTree nav array for any changes.
|
---|
24 | // When the array gets populated with values, build the flattened array for search
|
---|
25 | $scope.$watchCollection('fullTree', function (nav) {
|
---|
26 | if (nav.length > 0) {
|
---|
27 | flattenJsonArray($scope.fullTree, $scope.flattenedTree, "Children", ["Children"]);
|
---|
28 | //console.log("Nav tree is flattened");
|
---|
29 | }
|
---|
30 | });
|
---|
31 |
|
---|
32 | // Flattens an array using recursion.
|
---|
33 | // recursivePropertyName = the name of the property to call the recursion on e.g. "children"
|
---|
34 | // propertiesToRemoveArray = an array of properties to exclude from the flattened array e.g. ["children", "url"]
|
---|
35 | function flattenJsonArray(originalArray, flattenedArray, recursivePropertyName, propertiesToRemoveArray) {
|
---|
36 | if (!originalArray) return;
|
---|
37 |
|
---|
38 | var len = originalArray.length, i = 0; // cache array size for better speed
|
---|
39 | for (i; i < len; i++) {
|
---|
40 | var item = originalArray[i];
|
---|
41 | var newItem = angular.extend({}, item);
|
---|
42 | if (propertiesToRemoveArray) {
|
---|
43 | var innerLen = propertiesToRemoveArray.length, j = 0;
|
---|
44 | for (j; j < innerLen; j++) {
|
---|
45 | //delete newItem[propertiesToRemoveArray[j]]; // slower
|
---|
46 | newItem[propertiesToRemoveArray[j]] = null;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | flattenedArray.push(newItem);
|
---|
50 | flattenJsonArray(item[recursivePropertyName], flattenedArray, recursivePropertyName, propertiesToRemoveArray);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }]);
|
---|
54 | }()); |
---|