From 7d5ebe9ef36e448ffb9516f4ae65a9ebfccec371 Mon Sep 17 00:00:00 2001 From: Indri Muska Date: Wed, 11 Oct 2017 20:43:22 +0200 Subject: [PATCH] Bump up to 1.6.0 --- bower.json | 2 +- dist/angular-selector.css | 2 +- dist/angular-selector.js | 59 +++++++++++++++++++++++------------ dist/angular-selector.min.css | 2 +- dist/angular-selector.min.js | 4 +-- package.json | 4 +-- 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/bower.json b/bower.json index bca3c43..f42a2cc 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-selector", - "version": "1.5.0", + "version": "1.6.0", "authors": [ "Indri Muska " ], diff --git a/dist/angular-selector.css b/dist/angular-selector.css index 03d4385..2476068 100644 --- a/dist/angular-selector.css +++ b/dist/angular-selector.css @@ -1,4 +1,4 @@ -/*! angular-selector - v1.5.0 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ +/*! angular-selector - v1.6.0 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ @-webkit-keyframes selector-rotate { 0% { -webkit-transform: rotateZ(-359deg); } 100% { -webkit-transform: rotateZ(0deg); } } @-moz-keyframes selector-rotate { 0% { -moz-transform: rotateZ(-359deg); } 100% { -moz-transform: rotateZ(0deg); } } @-o-keyframes selector-rotate { 0% { -o-transform: rotateZ(-359deg); } 100% { -o-transform: rotateZ(0deg); } } diff --git a/dist/angular-selector.js b/dist/angular-selector.js index c29ef2f..fdf2665 100644 --- a/dist/angular-selector.js +++ b/dist/angular-selector.js @@ -1,4 +1,4 @@ -/*! angular-selector - v1.5.0 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ +/*! angular-selector - v1.6.0 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ (function (angular) { // Key codes @@ -81,6 +81,7 @@ remoteParam: 'q', remoteValidationParam: 'value', removeButton: true, + showCreateOption: false, viewItemTemplate: 'selector/item-default.html', dropdownItemTemplate: 'selector/item-default.html', dropdownCreateTemplate: 'selector/item-create.html', @@ -189,13 +190,13 @@ if (!angular.isDefined(scope.remoteValidation)) scope.remoteValidation = false; if (scope.remote) - $timeout(function () { + scope.$evalAsync(function () { $q.when(!scope.hasValue() || !scope.remoteValidation ? angular.noop : scope.fetchValidation(scope.value) ).then(function () { scope.$watch('search', function () { - $timeout(scope.fetch); + scope.$evalAsync(scope.fetch); }); }); }); @@ -276,7 +277,7 @@ if (scope.multiple && (scope.selectedValues || []).length >= scope.limit) return; scope.isOpen = true; scope.dropdownPosition(); - $timeout(scope.scrollToHighlighted); + scope.$evalAsync(scope.scrollToHighlighted); }; scope.close = function () { scope.isOpen = false; @@ -292,15 +293,17 @@ scope.scrollToHighlighted(); }; scope.highlight = function (index) { - if (attrs.create && scope.search && index == -1) - scope.highlighted = -1; - else - if (scope.filteredOptions.length) - scope.highlighted = (scope.filteredOptions.length + index) % scope.filteredOptions.length; + var length = scope.filteredOptions.length; + if (length) scope.highlighted = (length + index) % length; + if (scope.showCreateOption) { + if (index == -1 || index == length) scope.highlighted = -1; + if (index == -2) scope.highlighted = length - 1; + } }; scope.scrollToHighlighted = function () { var dd = dropdown[0], - option = dd.querySelectorAll('li.selector-option')[scope.highlighted], + index = scope.highlighted + (scope.showCreateOption ? 1 : 0), + option = dd.querySelectorAll('li.selector-option')[index], styles = getStyles(option), marginTop = parseFloat(styles.marginTop || 0), marginBottom = parseFloat(styles.marginBottom || 0); @@ -308,12 +311,12 @@ if (!scope.filteredOptions.length) return; if (option.offsetTop + option.offsetHeight + marginBottom > dd.scrollTop + dd.offsetHeight) - $timeout(function () { + scope.$evalAsync(function () { dd.scrollTop = option.offsetTop + option.offsetHeight + marginBottom - dd.offsetHeight; }); if (option.offsetTop - marginTop < dd.scrollTop) - $timeout(function () { + scope.$evalAsync(function () { dd.scrollTop = option.offsetTop - marginTop; }); }; @@ -358,8 +361,8 @@ scope.keydown = function (e) { switch (e.keyCode) { case KEYS.up: - if (!scope.isOpen) break; - scope.decrementHighlighted(); + if (!scope.isOpen) scope.open(); + else scope.decrementHighlighted(); e.preventDefault(); break; case KEYS.down: @@ -387,7 +390,7 @@ scope.unset(); scope.open(); if (scope.softDelete && !scope.disableSearch) - $timeout(function () { + scope.$evalAsync(function () { scope.search = search; }); e.preventDefault(); @@ -434,6 +437,14 @@ var index = scope.filteredOptions.indexOf(scope.selectedValues[0]); if (index >= 0) scope.highlight(index); } + // show create item option + if (scope.create) { + scope.showCreateOption = scope.create && scope.search && scope.filteredOptions.filter(function (option) { + return scope.getObjValue(option, scope.labelAttr).toLowerCase() == scope.search.toLowerCase(); + + }).length == 0; + if (scope.showCreateOption && scope.filteredOptions.length == 0 && scope.highlighted != -1) scope.highlighted = -1; + } }; // Input width utilities @@ -457,7 +468,7 @@ scope.resetInput = function () { input.val(''); scope.setInputWidth(); - $timeout(function () { scope.search = ''; }); + scope.$evalAsync(function () { scope.search = ''; }); }; scope.$watch('[search, options, value]', function () { @@ -512,15 +523,16 @@ }, true); // DOM event listeners + angular.element(element[0].querySelector('.selector-input')) + .on('click', function () { + input[0].focus(); + }); input = angular.element(element[0].querySelector('.selector-input input')) .on('focus', function () { $timeout(function () { scope.$apply(scope.open); }); }) - .on('blur', function () { - scope.$apply(scope.close); - }) .on('keydown', function (e) { scope.$apply(function () { scope.keydown(e); @@ -536,6 +548,13 @@ angular.element($window) .on('resize', function () { scope.dropdownPosition(); + }) + .on('click', function (e) { + if (angular.equals(element, e.target)) return; + if (scope.isOpen) { + scope.$evalAsync(scope.close); + e.preventDefault(); + } }); // Update select controller @@ -598,7 +617,7 @@ '' + '' + '