Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ClearOnRemove functionality #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/ion-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
this.loadingIcon = valueOrDefault($attrs.loadingIcon, undefined);
this.manageExternally = valueOrDefault($attrs.manageExternally, "false");
this.clearOnSelect = valueOrDefault($attrs.clearOnSelect, "true");
this.ngModelOptions = valueOrDefault($scope.$eval($attrs.ngModelOptions), {});
this.clearOnRemove = valueOrDefault($attrs.clearOnRemove, "false");
this.ngModelOptions = valueOrDefault($scope.$eval($attrs.ngModelOptions), {});
this.openClass = valueOrDefault($attrs.openClass, 'ion-autocomplete-open');
this.closeClass = valueOrDefault($attrs.closeClass, 'ion-autocomplete-close');

Expand Down Expand Up @@ -161,7 +162,7 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [

// store the selected items
if (!isKeyValueInObjectArray(ionAutocompleteController.selectedItems,
ionAutocompleteController.itemValueKey, ionAutocompleteController.getItemValue(item, ionAutocompleteController.itemValueKey))) {
ionAutocompleteController.itemValueKey, ionAutocompleteController.getItemValue(item, ionAutocompleteController.itemValueKey))) {

// if it is a single select set the item directly
if (ionAutocompleteController.maxSelectedItems == "1") {
Expand Down Expand Up @@ -200,6 +201,12 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
// clear the selected items if just one item is selected
if (!angular.isArray(ionAutocompleteController.selectedItems)) {
ionAutocompleteController.selectedItems = [];
if (ionAutocompleteController.clearOnRemove == "true") {
ionAutocompleteController.searchQuery = undefined;
ionAutocompleteController.cancelClick();
}


} else {
// remove the item from the selected items and create a copy of the array to update the model.
// See https://github.com/angular-ui/ui-select/issues/191#issuecomment-55471732
Expand Down Expand Up @@ -251,7 +258,7 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
// show the loading icon
ionAutocompleteController.showLoadingIcon = true;

var queryObject = {query: query, isInitializing: isInitializing};
var queryObject = { query: query, isInitializing: isInitializing };

// if the component id is set, then add it to the query object
if (ionAutocompleteController.componentId) {
Expand Down Expand Up @@ -350,7 +357,7 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
var onTouchStart = function (e) {
scrolling.moved = false;
// Use originalEvent when available, fix compatibility with jQuery
if (typeof(e.originalEvent) !== 'undefined') {
if (typeof (e.originalEvent) !== 'undefined') {
e = e.originalEvent;
}
scrolling.startX = e.touches[0].clientX;
Expand All @@ -360,7 +367,7 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
// check if the finger moves more than 10px and set the moved flag to true
var onTouchMove = function (e) {
// Use originalEvent when available, fix compatibility with jQuery
if (typeof(e.originalEvent) !== 'undefined') {
if (typeof (e.originalEvent) !== 'undefined') {
e = e.originalEvent;
}
if (Math.abs(e.touches[0].clientX - scrolling.startX) > 10 ||
Expand Down Expand Up @@ -402,7 +409,7 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
// function to call the model to item method and select the item
var resolveAndSelectModelItem = function (modelValue) {
// convert the given function to a $q promise to support promises too
var promise = $q.when(ionAutocompleteController.modelToItemMethod({modelValue: modelValue}));
var promise = $q.when(ionAutocompleteController.modelToItemMethod({ modelValue: modelValue }));

promise.then(function (promiseData) {
// select the item which are returned by the model to item method
Expand Down Expand Up @@ -483,7 +490,7 @@ angular.module('ion-autocomplete', []).directive('ionAutocomplete', [
// set the model value of the model
ngModelController.$parsers.push(function (viewValue) {
return ionAutocompleteController.getItemValue(viewValue, ionAutocompleteController.itemValueKey);
});
});

});

Expand Down