Skip to content

Commit 273a114

Browse files
committed
Merge pull request #10 from infomofo/global_controller
move global vars related to search, sidebar, and toasts to global.js
2 parents a6d20f1 + 4cbcb80 commit 273a114

File tree

4 files changed

+196
-123
lines changed

4 files changed

+196
-123
lines changed

app/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<link rel="stylesheet" href="styles/main.css">
1919
<!-- endbuild -->
2020
</head>
21-
<body layout="row" ng-app="yoAngularCordovaApp">
21+
<body layout="row" ng-app="yoAngularCordovaApp" ng-controller="GlobalCtrl">
2222

2323
<!--[if lt IE 7]>
2424
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade
@@ -56,6 +56,7 @@
5656
<!-- build:js({.tmp,app}) scripts/scripts.js -->
5757
<script src="scripts/app.js"></script>
5858
<script src="scripts/rootscope.js"></script>
59+
<script src="scripts/controllers/global.js"></script>
5960
<script src="scripts/controllers/main.js"></script>
6061
<script src="scripts/controllers/about.js"></script>
6162
<!-- endbuild -->

app/scripts/controllers/global.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
'use strict';
2+
3+
/**
4+
* @ngdoc function
5+
* @name yoAngularCordovaApp.controller:GlobalCtrl
6+
* @description
7+
* # GlobalCtrl
8+
* Controller of the yoAngularCordovaApp
9+
*/
10+
angular.module('yoAngularCordovaApp')
11+
.controller('GlobalCtrl', function ($scope, $rootScope, $mdSidenav, $mdToast){
12+
13+
$scope.searchActive = false;
14+
15+
/**
16+
* Closes the mdSidenav and handles any related behavior
17+
*/
18+
$scope.closeSideNav = function(){
19+
$mdSidenav('left').close()
20+
.then(function(){
21+
//console.debug("toggle left is done");
22+
});
23+
};
24+
25+
/**
26+
* A replacement for the sidenav toggle button if it is replaced with a hamburger action.
27+
*/
28+
$scope.handleHamburger = function() {
29+
if ($rootScope.isHistoryEmpty()) {
30+
$scope.toggleSideNav();
31+
} else {
32+
$rootScope.backFunction(true);
33+
}
34+
};
35+
36+
$scope.openSidenav = function() {
37+
$mdToast.hide();
38+
console.log('toggling left');
39+
$mdSidenav('left').open()
40+
.then(function(){
41+
console.debug('toggle left is done');
42+
});
43+
};
44+
45+
$scope.disableSearch = function() {
46+
//var searchBox = angular.element('#searchBox');
47+
//searchBox.blur();
48+
$scope.searchActive = false;
49+
};
50+
51+
/**
52+
* Closes the mdSidenav and handles any related behavior
53+
*/
54+
$scope.closeSideNav = function(){
55+
$mdSidenav('left').close()
56+
.then(function(){
57+
//console.debug("toggle left is done");
58+
});
59+
};
60+
61+
/**
62+
* A replacement for the sidenav toggle button if it is replaced with a hamburger action.
63+
*/
64+
$scope.handleHamburger = function() {
65+
if ($rootScope.isHistoryEmpty()) {
66+
$scope.toggleSideNav();
67+
} else {
68+
$rootScope.backFunction(true);
69+
}
70+
};
71+
72+
/**
73+
* Handles the event of clicking on the nav bar- this is a common application convention that will make the active
74+
* scrollable container scroll to the top.
75+
*/
76+
$scope.$on('NavClicked', function() {
77+
var domElement = document.getElementById('scrollcontainer');
78+
domElement.style.overflow = 'hidden';
79+
// wait for any current momentum scrolling to finish and then jump to top
80+
//$('#scrollcontainer').animate({scrollTop: 0}, 'fast');
81+
domElement.style.overflow = '';
82+
});
83+
84+
/**
85+
* Displays the search box element on the toolbar
86+
*/
87+
$scope.showSearch = function() {
88+
//var searchBox = angular.element('#searchBox');
89+
//searchBox.focus();
90+
$scope.searchActive = true;
91+
};
92+
93+
/**
94+
* Displays an alert toast in the bottom right that disappears after 3 seconds
95+
*
96+
* Suitable for displaying short unactionable messages to the user
97+
*
98+
* @param message The alert message to display to the user
99+
*/
100+
$scope.showAlertToast = function(message) {
101+
var toast = $mdToast.simple()
102+
.content(message)
103+
.highlightAction(false)
104+
.position('bottom right')
105+
.hideDelay(2000);
106+
$mdToast.show(toast);
107+
};
108+
109+
/**
110+
* Displays an alert toast in the bottom right that disappears when dismissed by the user
111+
*
112+
* Suitable for displaying short unactionable messages to the user
113+
*
114+
* @param message The alert message to display to the user
115+
*/
116+
$scope.showAlertToastPersistent = function(message) {
117+
var toast = $mdToast.simple()
118+
.content(message)
119+
.highlightAction(false)
120+
.position('bottom right')
121+
.hideDelay(0);
122+
$mdToast.show(toast);
123+
};
124+
125+
/**
126+
* Displays an undoable toast in the bottom right that disappears after 3 seconds
127+
*
128+
* @param message the message to display to the user
129+
* @param callback the function to call when the undo action is clicked
130+
*/
131+
$scope.showUndoToast = function(message, callback) {
132+
var toast = $mdToast.simple()
133+
.content(message)
134+
.action('undo')
135+
.highlightAction(false)
136+
.position('bottom right')
137+
.hideDelay(2000);
138+
$mdToast.show(toast).then(function() {
139+
callback(true);
140+
});
141+
};
142+
143+
/**
144+
* Displays an undoable toast in the bottom right that disappears when dismissed by the user
145+
*
146+
* @param message the message to display to the user
147+
* @param callback the function to call when the undo action is clicked
148+
*/
149+
$scope.showUndoToastPersistent = function(message, callback) {
150+
var toast = $mdToast.simple()
151+
.content(message)
152+
.action('undo')
153+
.highlightAction(false)
154+
.position('bottom right')
155+
.hideDelay(0);
156+
$mdToast.show(toast).then(function() {
157+
callback(true);
158+
});
159+
};
160+
161+
});

app/scripts/rootScope.js

Lines changed: 7 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@
66
* This file is for all functions that need to be globally accessible or access shared memory between routes.
77
*/
88
angular.module('yoAngularCordovaApp')
9-
.run(function ($rootScope, $location, $mdSidenav, $mdToast) {
10-
11-
var searchActive = false;
12-
13-
$rootScope.openSidenav = function() {
14-
$mdToast.hide();
15-
console.log('toggling left');
16-
$mdSidenav('left').open()
17-
.then(function(){
18-
console.debug('toggle left is done');
19-
});
20-
};
9+
.run(function ($rootScope, $location, $mdToast, $mdSidenav) {
2110

2211
var history = [];
2312

@@ -30,19 +19,9 @@ angular.module('yoAngularCordovaApp')
3019
}
3120
});
3221

33-
var disableSearch = function() {
34-
var searchBox = angular.element('#searchBox');
35-
searchBox.blur();
36-
searchActive = false;
37-
};
38-
3922
var backFunction = function() {
40-
if (searchActive) {
41-
disableSearch();
42-
} else {
43-
var prevUrl = history.length > 1 ? history.splice(-2)[0] : '/';
44-
$location.path(prevUrl);
45-
}
23+
var prevUrl = history.length > 1 ? history.splice(-2)[0] : '/';
24+
$location.path(prevUrl);
4625
};
4726

4827
$rootScope.backFunction = function () {
@@ -92,27 +71,6 @@ angular.module('yoAngularCordovaApp')
9271
return history.length > 1;
9372
};
9473

95-
/**
96-
* Closes the mdSidenav and handles any related behavior
97-
*/
98-
$rootScope.closeSideNav = function(){
99-
$mdSidenav('left').close()
100-
.then(function(){
101-
//console.debug("toggle left is done");
102-
});
103-
};
104-
105-
/**
106-
* A replacement for the sidenav toggle button if it is replaced with a hamburger action.
107-
*/
108-
$rootScope.handleHamburger = function() {
109-
if ($rootScope.isHistoryEmpty()) {
110-
$rootScope.toggleSideNav();
111-
} else {
112-
$rootScope.backFunction(true);
113-
}
114-
};
115-
11674
/**
11775
* This function should be called to change views- this will retain history and encapsulate any other behaviors.
11876
* @param url
@@ -121,7 +79,10 @@ angular.module('yoAngularCordovaApp')
12179
// Hide any active toasts when the route changes
12280
$mdToast.hide();
12381

124-
$rootScope.closeSideNav();
82+
$mdSidenav('left').close()
83+
.then(function(){
84+
//console.debug("toggle left is done");
85+
});
12586
$location.path(url);
12687
};
12788

@@ -136,80 +97,4 @@ angular.module('yoAngularCordovaApp')
13697
//$('#scrollcontainer').animate({scrollTop: 0}, 'fast');
13798
domElement.style.overflow = '';
13899
});
139-
140-
/**
141-
* Displays the search box element on the toolbar
142-
*/
143-
$rootScope.showSearch = function() {
144-
var searchBox = angular.element('#searchBox');
145-
searchBox.focus();
146-
};
147-
148-
/**
149-
* Displays an alert toast in the bottom right that disappears after 3 seconds
150-
*
151-
* Suitable for displaying short unactionable messages to the user
152-
*
153-
* @param message The alert message to display to the user
154-
*/
155-
$rootScope.showAlertToast = function(message) {
156-
var toast = $mdToast.simple()
157-
.content(message)
158-
.highlightAction(false)
159-
.position('bottom right')
160-
.hideDelay(2000);
161-
$mdToast.show(toast);
162-
};
163-
164-
/**
165-
* Displays an alert toast in the bottom right that disappears when dismissed by the user
166-
*
167-
* Suitable for displaying short unactionable messages to the user
168-
*
169-
* @param message The alert message to display to the user
170-
*/
171-
$rootScope.showAlertToastPersistent = function(message) {
172-
var toast = $mdToast.simple()
173-
.content(message)
174-
.highlightAction(false)
175-
.position('bottom right')
176-
.hideDelay(0);
177-
$mdToast.show(toast);
178-
};
179-
180-
/**
181-
* Displays an undoable toast in the bottom right that disappears after 3 seconds
182-
*
183-
* @param message the message to display to the user
184-
* @param callback the function to call when the undo action is clicked
185-
*/
186-
$rootScope.showUndoToast = function(message, callback) {
187-
var toast = $mdToast.simple()
188-
.content(message)
189-
.action('undo')
190-
.highlightAction(false)
191-
.position('bottom right')
192-
.hideDelay(2000);
193-
$mdToast.show(toast).then(function() {
194-
callback(true);
195-
});
196-
};
197-
198-
/**
199-
* Displays an undoable toast in the bottom right that disappears when dismissed by the user
200-
*
201-
* @param message the message to display to the user
202-
* @param callback the function to call when the undo action is clicked
203-
*/
204-
$rootScope.showUndoToastPersistent = function(message, callback) {
205-
var toast = $mdToast.simple()
206-
.content(message)
207-
.action('undo')
208-
.highlightAction(false)
209-
.position('bottom right')
210-
.hideDelay(0);
211-
$mdToast.show(toast).then(function() {
212-
callback(true);
213-
});
214-
};
215100
});

test/spec/controllers/global.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
describe('Controller: MainCtrl', function () {
4+
5+
// load the controller's module
6+
beforeEach(module('yoAngularCordovaApp'));
7+
8+
var GlobalCtrl,
9+
scope;
10+
11+
// Initialize the controller and a mock scope
12+
beforeEach(inject(function ($controller, $rootScope) {
13+
scope = $rootScope.$new();
14+
GlobalCtrl = $controller('GlobalCtrl', {
15+
$scope: scope
16+
});
17+
}));
18+
19+
it('should attach a list of awesomeThings to the scope', function () {
20+
expect(scope.searchActive).toBe(false);
21+
scope.showSearch();
22+
expect(scope.searchActive).toBe(true);
23+
scope.disableSearch();
24+
expect(scope.searchActive).toBe(false);
25+
});
26+
});

0 commit comments

Comments
 (0)