Skip to content

Commit

Permalink
Merge pull request darylrowland#28 from leejsinclair/feature/auto-hid…
Browse files Browse the repository at this point in the history
…e-on-blur

UX behaviour for hiding search results on blur and selecting text on mouseup
  • Loading branch information
Daryl Rowland committed May 22, 2014
2 parents 25356a4 + a2cd86b commit 8a8c262
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions angucomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ angular.module('angucomplete', [] )
"minLengthUser": "@minlength",
"matchClass": "@matchclass"
},
template: '<div class="angucomplete-holder"><input id="{{id}}_value" ng-model="searchStr" type="text" placeholder="{{placeholder}}" class="{{inputClass}}"/><div id="{{id}}_dropdown" class="angucomplete-dropdown" ng-if="showDropdown"><div class="angucomplete-searching" ng-show="searching">Searching...</div><div class="angucomplete-searching" ng-show="!searching && (!results || results.length == 0)">No results found</div><div class="angucomplete-row" ng-repeat="result in results" ng-click="selectResult(result)" ng-mouseover="hoverRow()" ng-class="{\'angucomplete-selected-row\': $index == currentIndex}"><div ng-if="imageField" class="angucomplete-image-holder"><img ng-if="result.image && result.image != \'\'" ng-src="{{result.image}}" class="angucomplete-image"/><div ng-if="!result.image && result.image != \'\'" class="angucomplete-image-default"></div></div><div class="angucomplete-title" ng-if="matchClass" ng-bind-html="result.title"></div><div class="angucomplete-title" ng-if="!matchClass">{{ result.title }}</div><div ng-if="result.description && result.description != \'\'" class="angucomplete-description">{{result.description}}</div></div></div></div>',
template: '<div class="angucomplete-holder"><input id="{{id}}_value" ng-model="searchStr" type="text" placeholder="{{placeholder}}" class="{{inputClass}}" onmouseup="this.select();" ng-focus="resetHideResults()" ng-blur="hideResults()" /><div id="{{id}}_dropdown" class="angucomplete-dropdown" ng-if="showDropdown"><div class="angucomplete-searching" ng-show="searching">Searching...</div><div class="angucomplete-searching" ng-show="!searching && (!results || results.length == 0)">No results found</div><div class="angucomplete-row" ng-repeat="result in results" ng-click="selectResult(result)" ng-mouseover="hoverRow()" ng-class="{\'angucomplete-selected-row\': $index == currentIndex}"><div ng-if="imageField" class="angucomplete-image-holder"><img ng-if="result.image && result.image != \'\'" ng-src="{{result.image}}" class="angucomplete-image"/><div ng-if="!result.image && result.image != \'\'" class="angucomplete-image-default"></div></div><div class="angucomplete-title" ng-if="matchClass" ng-bind-html="result.title"></div><div class="angucomplete-title" ng-if="!matchClass">{{ result.title }}</div><div ng-if="result.description && result.description != \'\'" class="angucomplete-description">{{result.description}}</div></div></div></div>',

link: function($scope, elem, attrs) {
$scope.lastSearchTerm = null;
$scope.currentIndex = null;
$scope.justChanged = false;
$scope.searchTimer = null;
$scope.hideTimer = null;
$scope.searching = false;
$scope.pause = 500;
$scope.minLength = 3;
Expand Down Expand Up @@ -133,9 +134,20 @@ angular.module('angucomplete', [] )
});
}
}

}

$scope.hideResults = function() {
$scope.hideTimer = $timeout(function() {
$scope.showDropdown = false;
}, $scope.pause);
};

$scope.resetHideResults = function() {
if($scope.hideTimer) {
$timeout.cancel($scope.hideTimer);
};
};

$scope.hoverRow = function(index) {
$scope.currentIndex = index;
}
Expand Down

0 comments on commit 8a8c262

Please sign in to comment.