Skip to content

Commit d8b96aa

Browse files
committed
Addresses Issue #5, but having issues on mobile due to double-firing events. i
See related: angular/angular.js#6251
1 parent dc4f806 commit d8b96aa

File tree

11 files changed

+119
-15
lines changed

11 files changed

+119
-15
lines changed

app/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<script src="scripts/controllers/global.js"></script>
6060
<script src="scripts/controllers/main.js"></script>
6161
<script src="scripts/controllers/about.js"></script>
62+
<script src="scripts/controllers/search.js"></script>
6263
<!-- endbuild -->
6364
</body>
6465
</html>

app/scripts/app.js

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ app
5151
.when('/tabs', {
5252
templateUrl: 'views/tabs.html'
5353
})
54+
.when('/search/:query', {
55+
templateUrl: 'views/search.html',
56+
controller: 'SearchCtrl'
57+
})
5458
.otherwise({
5559
redirectTo: '/'
5660
});

app/scripts/controllers/global.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
angular.module('yoAngularCordovaApp')
1111
.controller('GlobalCtrl', function ($scope, $rootScope, $mdSidenav, $mdToast){
1212

13-
$scope.searchActive = false;
13+
$scope.searchModel = {
14+
searchActive: false,
15+
searchQuery: null
16+
};
1417

1518
$scope.clickNav = function() {
1619
// Publish an event that can be handled by directives
@@ -48,10 +51,19 @@ angular.module('yoAngularCordovaApp')
4851
});
4952
};
5053

54+
$scope.executeSearch = function() {
55+
var path = '/search/' + $scope.searchModel.searchQuery;
56+
console.debug('go to path ' + path);
57+
$scope.disableSearch();
58+
// lose focus on search bar
59+
$rootScope.go(path);
60+
};
61+
5162
$scope.disableSearch = function() {
5263
//var searchBox = angular.element('#searchBox');
5364
//searchBox.blur();
54-
$scope.searchActive = false;
65+
$scope.searchModel.searchActive = false;
66+
$scope.searchModel.searchQuery = null;
5567
};
5668

5769
/**
@@ -93,7 +105,15 @@ angular.module('yoAngularCordovaApp')
93105
$scope.showSearch = function() {
94106
//var searchBox = angular.element('#searchBox');
95107
//searchBox.focus();
96-
$scope.searchActive = true;
108+
$scope.searchModel.searchActive = true;
109+
};
110+
111+
$scope.toggleSearch = function() {
112+
if ($scope.searchModel.searchActive) {
113+
$scope.disableSearch();
114+
} else {
115+
$scope.showSearch();
116+
}
97117
};
98118

99119
/**

app/scripts/controllers/search.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
/**
4+
* @ngdoc function
5+
* @name yoAngularCordovaApp.controller:SearchCtrl
6+
* @description
7+
* # SearchCtrl
8+
* Controller of the yoAngularCordovaApp
9+
*/
10+
angular.module('yoAngularCordovaApp')
11+
.controller('SearchCtrl', function ($scope, $routeParams) {
12+
13+
$scope.searchModel = {
14+
query: null
15+
};
16+
17+
$scope.searchModel.query = $routeParams.query;
18+
19+
});

app/scripts/directives/scrolltotoponnavclick.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ angular.module('yoAngularCordovaApp')
1313
link: function (scope, element) {
1414
$rootScope.$on('NavClicked', function () {
1515
$timeout(function () {
16-
console.debug('NavClicked detected on ' + element[0].outerHTML + ' has scrollTop ' + element[0].scrollTop);
16+
//console.debug('NavClicked detected on ' + element[0].outerHTML + ' has scrollTop ' + element[0].scrollTop);
1717
element[0].scrollTop = 0;
1818
//$window.scrollTo(0,0);
1919
});

app/styles/main.css

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
.noscroll {
22
overflow: hidden;
33
}
4+
5+
.toolbar-item {
6+
-webkit-transition: all linear 0.5s;
7+
transition: all linear 0.5s;
8+
max-width:100%;
9+
overflow-x: hidden;
10+
white-space: nowrap;
11+
}
12+
13+
.toolbar-item.ng-hide {
14+
max-width:0;
15+
}
16+
17+
.toolbar-item.ng-enter, .toolbar-item.ng-leave {
18+
-webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
19+
transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
20+
}
21+
22+
.toolbar-item.ng-enter,
23+
.toolbar-item.ng-leave.ng-leave-active {
24+
max-width:0;
25+
}
26+
27+
.toolbar-item.ng-leave,
28+
.toolbar-item.ng-enter.ng-enter-active {
29+
opacity:1;
30+
max-width:100%;
31+
}

app/views/search.html

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>This is the search view.</p>
2+
<p>{{searchModel.query}}</p>

app/views/toolbar.html

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
<md-toolbar class="md-primary" md-scroll-shrink="false" layout-align="center center" ng-click="clickNav()">
2-
<h2 class="md-toolbar-tools" layout="row" layout-align="start end">
3-
<div layout-align="center center">
2+
<div class="md-toolbar-tools" layout="row" layout-align="space-between center">
3+
<div layout-align="center center" ng-if="!searchModel.searchActive" class="toolbar-item">
44
<md-button ng-click="openSidenav()" class="md-accent" hide-gt-md aria-label="toggles the sidenav">
55
<i class="fa fa-2x fa-align-justify"></i>
66
</md-button>
7+
<span>Cordova Angular MD App</span>
78
</div>
8-
<span> Toolbar</span>
9-
</h2>
9+
<md-button ng-click="toggleSearch()" class="md-accent" aria-label="toggles the search field">
10+
<i class="fa fa-2x fa-search"></i>
11+
</md-button>
12+
<div class="toolbar-item" ng-if="searchModel.searchActive" layout-fill flex layout="row" md-no-ink>
13+
<form ng-submit="executeSearch()" layout-fill flex>
14+
<md-input-container flex class="md-accent">
15+
<label>Search term</label>
16+
<input id="searchBox" autofocus type="search" ng-model="searchModel.searchQuery" class="md-accent"/>
17+
</md-input-container>
18+
</form>
19+
<md-button ng-show="searchModel.searchQuery" class="md-accent" ng-click="searchModel.searchQuery=null">
20+
<i class="fa fa-2x fa-times"></i>
21+
</md-button>
22+
</div>
23+
</div>
1024
</md-toolbar>

test/spec/controllers/global.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
describe('Controller: MainCtrl', function () {
3+
describe('Controller: GlobalCtrl', function () {
44

55
// load the controller's module
66
beforeEach(module('yoAngularCordovaApp'));
@@ -17,10 +17,10 @@ describe('Controller: MainCtrl', function () {
1717
}));
1818

1919
it('should attach a list of awesomeThings to the scope', function () {
20-
expect(scope.searchActive).toBe(false);
20+
expect(scope.searchModel.searchActive).toBe(false);
2121
scope.showSearch();
22-
expect(scope.searchActive).toBe(true);
22+
expect(scope.searchModel.searchActive).toBe(true);
2323
scope.disableSearch();
24-
expect(scope.searchActive).toBe(false);
24+
expect(scope.searchModel.searchActive).toBe(false);
2525
});
2626
});

test/spec/controllers/main.js

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,4 @@ describe('Controller: MainCtrl', function () {
1616
});
1717
}));
1818

19-
it('should attach a list of awesomeThings to the scope', function () {
20-
expect(scope.awesomeThings.length).toBe(3);
21-
});
2219
});

test/spec/controllers/search.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
describe('Controller: SearchCtrl', function () {
4+
5+
// load the controller's module
6+
beforeEach(module('yoAngularCordovaApp'));
7+
8+
var SearchCtrl,
9+
scope;
10+
11+
// Initialize the controller and a mock scope
12+
beforeEach(inject(function ($controller, $rootScope) {
13+
scope = $rootScope.$new();
14+
SearchCtrl = $controller('SearchCtrl', {
15+
$scope: scope
16+
});
17+
}));
18+
19+
});

0 commit comments

Comments
 (0)