nitweb: close quick search box when clicking outside
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 17 Jan 2017 20:53:46 +0000 (15:53 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 17 Jan 2017 20:53:46 +0000 (15:53 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

share/nitweb/directives/search/field.html [new file with mode: 0644]
share/nitweb/index.html
share/nitweb/javascripts/ui.js

diff --git a/share/nitweb/directives/search/field.html b/share/nitweb/directives/search/field.html
new file mode 100644 (file)
index 0000000..d9ee5f1
--- /dev/null
@@ -0,0 +1,13 @@
+<form>
+       <div class='form-group has-icon'>
+               <input placeholder='Search...' type='text' class='form-control search-input'
+                       ng-model-options='{ debounce: 300 }' ng-model='query'
+                       ng-keydown='update($event)' ng-change='search()'>
+               <span class='glyphicon glyphicon-search form-control-icon text-muted'></span>
+       </div>
+       <div ng-if='results.length > 0' class='search-results'>
+               <div class='card-list'>
+                       <search-card ng-click='selectEnter()' ng-class='{active: activeItem == $index}' ng-mouseover='setActive($index)' mentity='mentity' ng-repeat='mentity in results' />
+               </div>
+       </div>
+</form>
index 1117ae7..138955b 100644 (file)
                                        </div>
                                </div>
                                <div class='col-xs-7'>
-                                       <form ng-controller='SearchCtrl as searchCtrl' >
-                                               <div class='form-group has-icon'>
-                                                       <input placeholder='Search...' type='text' class='form-control search-input'
-                                                               ng-model-options='{ debounce: 300 }' ng-model='query'
-                                                               ng-keydown='update($event)' ng-change='search()'>
-                                                       <span class='glyphicon glyphicon-search form-control-icon text-muted'></span>
-                                               </div>
-                                               <div ng-if='results.length > 0' class='search-results'>
-                                                       <div class='card-list'>
-                                                               <search-card ng-click='selectEnter()' ng-class='{active: activeItem == $index}' ng-mouseover='setActive($index)' mentity='mentity' ng-repeat='mentity in results' />
-                                                       </div>
-                                               </div>
-                                       </form>
+                                       <search-field />
                                </div>
                                <div class='col-xs-2'>
                                        <user-menu />
index e9f91d2..f6f5eca 100644 (file)
@@ -18,7 +18,8 @@
        angular
                .module('ui', [ 'model' ])
 
-               .controller('SearchCtrl', ['Model', '$routeParams', '$scope', '$location', function(Model, $routeParams, $scope, $location) {
+               .controller('SearchCtrl', function(Model, $routeParams, $scope, $location, $document) {
+
                        $scope.query = '';
 
                        $scope.reset = function() {
                        }
 
                        $scope.reset();
-               }])
+               })
+
+               .directive('searchField', function($document) {
+                       return {
+                               restrict: 'E',
+                               replace: true,
+                               controller: 'SearchCtrl',
+                               controllerAs: 'searchCtrl',
+                               templateUrl: '/directives/search/field.html',
+                               link: function ($scope, element, attrs) {
+                                       $document.bind('click', function (event) {
+                                               var isChild = $(element).has(event.target).length > 0;
+                                               var isSelf = element[0] == event.target;
+                                               var isInside = isChild || isSelf;
+                                               if (!isInside) {
+                                                       $scope.reset();
+                                                       $scope.$apply();
+                                               }
+                                       });
+                               }
+                       };
+               })
 
                .directive('searchCard', function() {
                        return {