Service Data and submenus

James Peret 9 years ago
parent
commit
9ed2897930

+ 1 - 0
app/index.html

@@ -30,6 +30,7 @@
30 30
     <script src="scripts/controllers/background.js"></script>
31 31
     <script src="scripts/controllers/navigation.js"></script>
32 32
     <script src="scripts/controllers/slideshow.js"></script>
33
+    <script src="scripts/services/data.js"></script>
33 34
 
34 35
 </head>
35 36
 <body ng-app="goApp">

+ 1 - 0
app/scripts/app.js

@@ -18,6 +18,7 @@ angular
18 18
     'ngTouch',
19 19
     'famous.angular',
20 20
     'ngFx',
21
+    'goApp.data',
21 22
     'goApp.index',
22 23
     'goApp.background',
23 24
     'goApp.navigation',

+ 39 - 4
app/scripts/controllers/navigation.js

@@ -7,8 +7,16 @@
7 7
  * # AboutCtrl
8 8
  * Controller of the domainManagerApp
9 9
  */
10
-angular.module('goApp.navigation', ['famous.angular', 'ngRoute', 'ngFx', 'ngAnimate'])
11
-  .controller('NavigationController',['$scope', '$rootScope', '$famous', '$timeline', '$http', '$location', '$route', '$timeout', function ($scope,  $rootScope, $famous, $timeline, $http, $location, $route, $timeout) {
10
+angular.module('goApp.navigation', ['famous.angular', 'ngRoute', 'ngFx', 'ngAnimate', 'goApp.data'])
11
+  .controller('NavigationController',['$scope', '$rootScope', '$famous', '$timeline', '$http', '$location', '$route', '$timeout', 'Data', function ($scope,  $rootScope, $famous, $timeline, $http, $location, $route, $timeout, Data) {
12
+
13
+    $rootScope.$on('data:loaded', function(data) {
14
+      if(!$scope.$$phase) {
15
+        $scope.$apply(function(){
16
+          $scope.navigation = Data.get();
17
+        });
18
+      }
19
+    });
12 20
 
13 21
     $http({
14 22
       method: 'GET',
@@ -22,28 +30,55 @@ angular.module('goApp.navigation', ['famous.angular', 'ngRoute', 'ngFx', 'ngAnim
22 30
     $scope.subNavigation = []
23 31
     $scope.sub2Navigation = []
24 32
     $scope.last_click = ""
33
+    $scope.last_click2 = ""
25 34
 
26 35
     $scope.btnNavclick = function(index){
27 36
       if($scope.navigation[index].link == 'submenu') {
28 37
         if($scope.last_click != $scope.navigation[index].title){
29 38
           console.log("Opening submenu: " + $scope.navigation[index].title)
39
+          $scope.sub2Navigation = [];
30 40
           $scope.subNavigation = $scope.navigation[index].submenu;
31 41
         } else if ($scope.last_click == $scope.navigation[index].title && $scope.subNavigation.length == 0) {
32 42
           $scope.subNavigation = $scope.navigation[index].submenu;
33 43
           console.log("Opening submenu: " + $scope.navigation[index].title)
34 44
         } else {
35 45
           $scope.subNavigation = [];
46
+          $scope.sub2Navigation = [];
36 47
           console.log("Closing submenu: " + $scope.navigation[index].title)
37 48
         }
38 49
         $scope.last_click = $scope.navigation[index].title
39 50
       } else {
40
-        console.log("Redirecting to page " + $scope.navigation[index].link)
51
+        console.log("> Redirecting to page " + $scope.navigation[index].link)
41 52
         $location.path($scope.navigation[index].link);
42 53
       }
43 54
     }
44 55
 
45 56
     $scope.btnSubNavclick = function(index){
46
-      $scope.sub2Navigation = $scope.subNavigation[index].submenu;
57
+      if($scope.subNavigation[index].link == "submenu") {
58
+        if($scope.last_click2 != $scope.subNavigation[index].title){
59
+          $scope.sub2Navigation = [];
60
+          $scope.sub2Navigation = $scope.subNavigation[index].submenu;
61
+        } else if ($scope.last_click2 == $scope.subNavigation[index].title && $scope.sub2Navigation.length == 0){
62
+          $scope.sub2Navigation = $scope.subNavigation[index].submenu;
63
+          console.log("Opening submenu: " + $scope.subNavigation[index].title)
64
+        } else {
65
+          $scope.sub2Navigation = [];
66
+          console.log("Closing submenu: " + $scope.subNavigation[index].title)
67
+        }
68
+        $scope.last_click2 = $scope.subNavigation[index].title
69
+      } else {
70
+        console.log("> Redirecting to page " + $scope.subNavigation[index].link)
71
+        $location.path($scope.subNavigation[index].link);
72
+      }
73
+    }
74
+
75
+    $scope.btnSub2Navclick = function(index) {
76
+      if($scope.sub2Navigation[index].link != "submenu") {
77
+        console.log("> Redirecting to page " + $scope.sub2Navigation[index].link)
78
+        $location.path($scope.sub2Navigation[index].link);
79
+      } else {
80
+        console.log("* Error: Only 2 levels of submenu allowed.")
81
+      }
47 82
     }
48 83
 
49 84
   }]);

+ 15 - 4
app/scripts/controllers/slideshow.js

@@ -7,11 +7,19 @@
7 7
  * # AboutCtrl
8 8
  * Controller of the domainManagerApp
9 9
  */
10
-angular.module('goApp.slideshow', ['ngFx', 'ngAnimate'])
11
-  .controller('SlideshowController',['$scope', '$rootScope', '$famous', '$timeline', '$location', '$route', '$timeout', function ($scope,  $rootScope, $famous, $timeline, $location, $route, $timeout) {
12
-
13
-    $scope.data = {message: "Hello World"};
10
+angular.module('goApp.slideshow', ['ngFx', 'ngAnimate', 'goApp.data'])
11
+  .controller('SlideshowController',['$scope', '$rootScope', '$famous', '$timeline', '$location', '$route', '$timeout', 'Data', function ($scope,  $rootScope, $famous, $timeline, $location, $route, $timeout, Data) {
14 12
 
13
+    var link = $location.url()
14
+    $scope.pageData = Data.getPageData(link);
15
+    $rootScope.$on('data:loaded', function(data) {
16
+      if(!$scope.$$phase) {
17
+        $scope.$apply(function(){
18
+          $scope.pageData = Data.getPageData(link);
19
+        });
20
+      }
21
+    });
22
+    
15 23
     // FadeIn
16 24
     $timeout(function(){
17 25
       $scope.showView = true;
@@ -51,4 +59,7 @@ angular.module('goApp.slideshow', ['ngFx', 'ngAnimate'])
51 59
 
52 60
 
53 61
 
62
+    $scope.pageData = Data.getPageData();
63
+    $scope.startAnimations();
64
+
54 65
   }]);

+ 26 - 15
app/scripts/data.json

@@ -9,11 +9,22 @@
9 9
       [
10 10
         {
11 11
           "title"   : "Caligramas",
12
-          "link"    : "#/caligramas"
12
+          "link"    : "/caligramas",
13
+          "slides"  :
14
+          [
15
+            {
16
+              "title" : "Caligrama 1",
17
+              "url"   : "/content/images/A3.jpg"
18
+            },
19
+            {
20
+              "title" : "Caligrama 2",
21
+              "url"   : "/content/images/cave-1.jpg"
22
+            }
23
+          ]
13 24
         },
14 25
         {
15 26
           "title"   : "Figuras",
16
-          "link"    : "#/figuras"
27
+          "link"    : "/figuras"
17 28
         },
18 29
         {
19 30
           "title"   : "Cadernos",
@@ -22,15 +33,15 @@
22 33
           [
23 34
             {
24 35
               "title"   : "York Cathedral",
25
-              "link"    : "#/york-cathedral"
36
+              "link"    : "/york-cathedral"
26 37
             },
27 38
             {
28 39
               "title"   : "Bologramas",
29
-              "link"    : "#/bologramas"
40
+              "link"    : "/bologramas"
30 41
             },
31 42
             {
32 43
               "title"   : "Sarrafos & Garranchos",
33
-              "link"    : "#/sarrafos-e-garranchos"
44
+              "link"    : "/sarrafos-e-garranchos"
34 45
             }
35 46
           ]
36 47
         }
@@ -44,25 +55,25 @@
44 55
       [
45 56
         {
46 57
           "title" : "Carvão & Tule",
47
-          "link"  : "#/carvao-e-tule"
58
+          "link"  : "/carvao-e-tule"
48 59
         },
49 60
         {
50 61
           "title": "Sono (vidros)",
51
-          "link" : "#/sono"
62
+          "link" : "/sono"
52 63
         },
53 64
         {
54 65
           "title" : "Relevos",
55
-          "link"  : "#/relevos"
66
+          "link"  : "/relevos"
56 67
         },
57 68
         {
58 69
           "title" : "cut-outs",
59
-          "link"  : "#/cut-outs"
70
+          "link"  : "/cut-outs"
60 71
         }
61 72
       ]
62 73
     },
63 74
     {
64 75
       "title"  : "poemas",
65
-      "link"   : "#/poemas"
76
+      "link"   : "/poemas"
66 77
     },
67 78
     {
68 79
       "title"   : "filmes",
@@ -71,7 +82,7 @@
71 82
       [
72 83
         {
73 84
           "title" : "Trifonia",
74
-          "link"  : "#/trifonia"
85
+          "link"  : "/trifonia"
75 86
         },
76 87
         {
77 88
           "title" : "Sonho contra-sono",
@@ -79,21 +90,21 @@
79 90
         },
80 91
         {
81 92
           "title" : "Animações",
82
-          "link"  : "#/animacoes"
93
+          "link"  : "/animacoes"
83 94
         },
84 95
         {
85 96
           "title" : "Performances",
86
-          "link"  : "#/performances"
97
+          "link"  : "/performances"
87 98
         }
88 99
       ]
89 100
     },
90 101
     {
91 102
       "title" : "Fotos Contexto Humano",
92
-      "link"  : "#/fotos-contexto-humano"
103
+      "link"  : "/fotos-contexto-humano"
93 104
     },
94 105
     {
95 106
       "title" : "Textos Críticos",
96
-      "link"  : "#/textos-criticos"
107
+      "link"  : "/textos-criticos"
97 108
     },
98 109
     {
99 110
       "title" : "Currículo",

+ 53 - 0
app/scripts/services/data.js

@@ -0,0 +1,53 @@
1
+"use strict"
2
+
3
+angular.module('goApp.data', [])
4
+.service('Data', [ '$rootScope', '$http', function($rootScope, $http) {
5
+  var data = [];
6
+  var data_loaded = false;
7
+
8
+  $http({
9
+    method: 'GET',
10
+    url: 'scripts/data.json'
11
+  }).success(function(json_data) {
12
+    data = json_data;
13
+    data_loaded = true;
14
+    $rootScope.$broadcast('data:loaded', data);
15
+    console.log('> Loaded data.json');
16
+  });
17
+
18
+  this.get = function() {
19
+    return data;
20
+  }
21
+
22
+  this.getPageData = function(link){
23
+
24
+    if (data_loaded) {
25
+
26
+      var page_data = "";
27
+      for (var i = 0; i < data.navigation.length; i++) {
28
+        if(data.navigation[i].link == "submenu"){
29
+          for (var a = 0; a < data.navigation[i].submenu.length; a++) {
30
+            if(data.navigation[i].submenu[a].link == "submenu"){
31
+              for (var b = 0; b < data.navigation[i].submenu[a].submenu.length; b++) {
32
+                  if(data.navigation[i].submenu[a].submenu[b].link == link){
33
+                    page_data = data.navigation[i].submenu[a].submenu[b];
34
+                  }
35
+              }
36
+            } else if(data.navigation[i].submenu[a].link == link){
37
+              page_data = data.navigation[i].submenu[a]
38
+            }
39
+          }
40
+        } else if(data.navigation[i].link == link){
41
+          page_data = data.navigation[i];
42
+        }
43
+      }
44
+
45
+      if(page_data){
46
+        console.log(page_data);
47
+      }
48
+    }
49
+
50
+    return page_data;
51
+  }
52
+
53
+}]);