// Generated by CoffeeScript 1.12.7
/**
* Input completer for search text fields
* @module
*/
'use strict';
app.directive('inputCompleter', [
'constantService', function(constants) {
return {
restrict: 'E',
templateUrl: 'site/inputCompleter.html',
scope: {
value: '=ngModel',
completer: '&',
placeholder: '@',
pattern: '@',
submit: '&'
},
link: {
pre: function($scope, $element, $attrs) {
$scope.debounce = parseInt($attrs.debounce, 10);
if (!isFinite($scope.debounce)) {
$scope.debounce = 250;
}
},
post: function($scope, $element, $attrs) {
var handle, input, min, resend, sent, setValue;
input = $element[0].firstChild;
if ($attrs.inputId) {
input.setAttribute('id', $attrs.inputId);
}
input.setAttribute('name', $attrs.inputName);
if (!isFinite(min = parseInt($attrs.min))) {
min = 3;
}
sent = resend = void 0;
$scope.choices = [];
$scope.selected = void 0;
setValue = function(v) {
var old;
old = input.value;
$scope.value = input.value = v;
if (v.toLowerCase().startsWith(old.toLowerCase())) {
input.setSelectionRange(old.length, v.length);
}
};
handle = function(r) {
if (r && typeof r.then === 'function') {
r.then(handle, function() {
return sent = void 0;
});
} else {
sent = void 0;
if (Array.isArray(r)) {
if ('input' in r) {
setValue(r.input);
delete r.input;
}
if (r.length) {
$scope.choices = r;
} else {
$scope.choices = [
{
text: constants.message('search.none')
}
];
}
} else if (r || r === '') {
setValue(r);
$scope.choices = [];
}
if (resend) {
$scope.search(resend);
}
}
};
$scope.search = function(value) {
if (value == null) {
value = input.value;
}
$scope.selected = void 0;
if (sent) {
resend = value !== sent && value;
} else if ((value != null ? value.length : void 0) >= min) {
resend = void 0;
sent = value;
$scope.choices.push({
text: constants.message('search.active')
});
handle($scope.completer({
$input: value
}));
} else {
$scope.choices = [];
}
};
$scope.choose = function(c, event) {
$scope.selected = void 0;
resend = void 0;
handle(typeof c.select === 'function' ? c.select(event) : c.select);
};
$scope.enter = function($event) {
var value;
if ($scope.selected != null) {
$scope.choose($scope.choices[$scope.selected], $event);
return;
}
$scope.value = value = input.value;
if ((value != null ? value.length : void 0) >= min) {
handle($scope.submit({
$event: $event,
$input: value
}));
}
};
$scope.select = function(i) {
var c, ul;
if (!$scope.choices.length) {
return;
}
if ($scope.selected == null) {
$scope.selected = -(i > 0);
}
$scope.selected += i;
$scope.selected %= $scope.choices.length;
if ($scope.selected < 0) {
$scope.selected += $scope.choices.length;
}
c = $scope.choices[$scope.selected];
if (typeof c.select === 'string') {
setValue(c.select);
}
ul = $element.children('ul');
ul.scrollTop(ul.children('li')[$scope.selected].offsetTop);
};
$scope.search(input.value || $scope.value);
}
}
};
}
]);
//# sourceMappingURL=inputCompleter.js.map