1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| /**
*
*/
'use strict';
angular.module('az.fr.directives')
.directive('autocomplete', function ($parse, $http, $sce, $timeout) {
return {
restrict: 'EA',
scope: {
"id": "@id",
"placeholder": "@placeholder",
"selectedObject": "=selectedobject",
"url": "@url",
"dataField": "@datafield",
"titleField": "@titlefield",
"descriptionField": "@descriptionfield",
"imageField": "@imagefield",
"imageUri": "@imageuri",
"inputClass": "@inputclass",
"userPause": "@pause",
"localData": "=localdata",
"searchFields": "@searchfields",
"minLengthUser": "@minlength",
"matchClass": "@matchclass"
},
template: '<div class="autocomplete-holder"><input id="{{id}}_value" ng-model="searchStr" type="text" autocomplete="off" placeholder="{{placeholder}}" class="{{inputClass}}" onmouseup="this.select();" ng-focus="resetHideResults()" ng-blur="hideResults()" /><div id="{{id}}_dropdown" class="autocomplete-dropdown" ng-if="showDropdown"><div class="autocomplete-searching" ng-show="searching">Searching...</div><div class="autocomplete-searching" ng-show="!searching && (!results || results.length == 0)">No results found</div><div class="autocomplete-row" ng-repeat="result in results" ng-mousedown="selectResult(result)" ng-mouseover="hoverRow()" ng-class="{\'autocomplete-selected-row\': $index == currentIndex}"><div ng-if="imageField" class="autocomplete-image-holder"><img ng-if="result.image && result.image != \'\'" ng-src="{{result.image}}" class="autocomplete-image"/><div ng-if="!result.image && result.image != \'\'" class="autocomplete-image-default"></div></div><div class="autocomplete-title" ng-if="matchClass" ng-bind-html="result.title"></div><div class="autocomplete-title" ng-if="!matchClass">{{ result.title }}</div><div ng-if="result.description && result.description != \'\'" class="autocomplete-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;
$scope.searchStr = $scope.selectedObject || null;
if ($scope.minLengthUser && $scope.minLengthUser != "") {
$scope.minLength = $scope.minLengthUser;
}
if ($scope.userPause) {
$scope.pause = $scope.userPause;
}
var isNewSearchNeeded = function(newTerm, oldTerm) {
return newTerm.length >= $scope.minLength && newTerm != oldTerm
}
$scope.processResults = function(responseData, str) {
if (responseData && responseData.length > 0) {
$scope.results = [];
var titleFields = [];
if ($scope.titleField && $scope.titleField != "") {
titleFields = $scope.titleField.split(",");
}
for (var i = 0; i < responseData.length; i++) {
// Get title variables
var titleCode = [];
for (var t = 0; t < titleFields.length; t++) {
titleCode.push(responseData[i][titleFields[t]]);
}
var description = "";
if ($scope.descriptionField) {
description = responseData[i][$scope.descriptionField];
}
var imageUri = "";
if ($scope.imageUri) {
imageUri = $scope.imageUri;
}
var image = "";
if ($scope.imageField) {
image = imageUri + responseData[i][$scope.imageField];
}
var text = titleCode.join(' ');
if ($scope.matchClass) {
var re = new RegExp(str, 'i');
var strPart = text.match(re)[0];
text = $sce.trustAsHtml(text.replace(re, '<span class="'+ $scope.matchClass +'">'+ strPart +'</span>'));
}
var resultRow = {
title: text,
description: description,
image: image,
originalObject: responseData[i]
}
$scope.results[$scope.results.length] = resultRow;
}
} else {
var resultRow = {
title: 'AUTRE MARQUE',
description: '',
image: '',
originalObject: ''
}
$scope.results[0] = resultRow;
}
}
$scope.searchTimerComplete = function(str) {
// Begin the search
if (str.length >= $scope.minLength) {
if ($scope.localData) {
var searchFields = $scope.searchFields.split(",");
var matches = [];
for (var i = 0; i < $scope.localData.length; i++) {
var match = false;
for (var s = 0; s < searchFields.length; s++) {
match = match || (typeof $scope.localData[i][searchFields[s]] === 'string' && typeof str === 'string' && $scope.localData[i][searchFields[s]].toLowerCase().indexOf(str.toLowerCase()) >= 0);
}
if (match) {
matches[matches.length] = $scope.localData[i];
}
}
$scope.searching = false;
$scope.processResults(matches, str);
} else {
$http.get($scope.url + str, {}).
success(function(responseData, status, headers, config) {
$scope.searching = false;
$scope.processResults((($scope.dataField) ? responseData[$scope.dataField] : responseData ), str);
}).
error(function(data, status, headers, config) {
console.log("error");
});
}
}
}
$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;
}
$scope.keyPressed = function(event) {
if (!(event.which == 38 || event.which == 40 || event.which == 13)) {
if (!$scope.searchStr || $scope.searchStr == "") {
$scope.showDropdown = false;
$scope.lastSearchTerm = null
} else if (isNewSearchNeeded($scope.searchStr, $scope.lastSearchTerm)) {
$scope.lastSearchTerm = $scope.searchStr
$scope.showDropdown = true;
$scope.currentIndex = -1;
$scope.results = [];
if ($scope.searchTimer) {
$timeout.cancel($scope.searchTimer);
}
$scope.searching = true;
$scope.searchTimer = $timeout(function() {
$scope.searchTimerComplete($scope.searchStr);
}, $scope.pause);
}
} else {
event.preventDefault();
}
}
$scope.selectResult = function(result) {
if ($scope.matchClass) {
result.title = result.title.toString().replace(/(<([^>]+)>)/ig, '');
}
$scope.searchStr = $scope.lastSearchTerm = result.title;
$scope.selectedObject = $scope.searchStr;
$scope.showDropdown = false;
$scope.results = [];
//$scope.$apply();
}
var inputField = elem.find('input');
inputField.on('keyup', $scope.keyPressed);
elem.on("keyup", function (event) {
if(event.which === 40) {
if ($scope.results && ($scope.currentIndex + 1) < $scope.results.length) {
$scope.currentIndex ++;
$scope.$apply();
event.preventDefault;
event.stopPropagation();
}
$scope.$apply();
} else if(event.which == 38) {
if ($scope.currentIndex >= 1) {
$scope.currentIndex --;
$scope.$apply();
event.preventDefault;
event.stopPropagation();
}
} else if (event.which == 13) {
if ($scope.results && $scope.currentIndex >= 0 && $scope.currentIndex < $scope.results.length) {
$scope.selectResult($scope.results[$scope.currentIndex]);
$scope.$apply();
event.preventDefault;
event.stopPropagation();
} else {
$scope.results = [];
$scope.$apply();
event.preventDefault;
event.stopPropagation();
}
} else if (event.which == 27) {
$scope.results = [];
$scope.showDropdown = false;
$scope.$apply();
} else if (event.which == 8) {
$scope.selectedObject = null;
$scope.$apply();
}
});
}
};
}); |
Partager