From f6c5cb81a75d0d72518e8bfd2ce5bb455e1f7a40 Mon Sep 17 00:00:00 2001 From: Vitor Fernandes Date: Thu, 28 May 2015 14:59:48 +0100 Subject: [PATCH] fix(select): correctly sort selected items using source item index Javascript array sort() uses alpha sort by default. This passes a function to the sort() call that will do a numeric sort instead, as suggested by @sumigoma. Fixes #1707 --- src/select/select.js | 2 +- src/select/test/select.spec.js | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/select/select.js b/src/select/select.js index 1d0f652e9..1538827ca 100644 --- a/src/select/select.js +++ b/src/select/select.js @@ -102,7 +102,7 @@ angular.module('mgcrea.ngStrap.select', ['mgcrea.ngStrap.tooltip', 'mgcrea.ngStr $select.activate = function(index) { if(options.multiple) { $select.$isActive(index) ? scope.$activeIndex.splice(scope.$activeIndex.indexOf(index), 1) : scope.$activeIndex.push(index); - if(options.sort) scope.$activeIndex.sort(); + if(options.sort) scope.$activeIndex.sort(function(a, b) { return a - b; }); // use numeric sort instead of default sort } else { scope.$activeIndex = index; } diff --git a/src/select/test/select.spec.js b/src/select/test/select.spec.js index 7346b4a72..5688aa0ad 100644 --- a/src/select/test/select.spec.js +++ b/src/select/test/select.spec.js @@ -97,7 +97,11 @@ describe('select', function () { element: '' }, 'options-multiple-sort': { - scope: {sort: true, selectedIcons: [], icons: [{value: 'Gear', label: '> Gear'}, {value: 'Globe', label: '> Globe'}, {value: 'Heart', label: '> Heart'}, {value: 'Camera', label: '> Camera'}]}, + scope: {sort: true, selectedIcons: [], icons: [ + {value: 'Gear0', label: '> Gear0'}, {value: 'Globe1', label: '> Globe1'}, {value: 'Heart2', label: '> Heart2'}, {value: 'Camera3', label: '> Camera3'}, + {value: 'Gear4', label: '> Gear4'}, {value: 'Globe5', label: '> Globe5'}, {value: 'Heart6', label: '> Heart6'}, {value: 'Camera7', label: '> Camera7'}, + {value: 'Gear8', label: '> Gear8'}, {value: 'Globe9', label: '> Globe9'}, {value: 'Heart10', label: '> Heart10'}, {value: 'Camera11', label: '> Camera11'} + ]}, element: '' }, 'options-container': { @@ -169,7 +173,7 @@ describe('select', function () { expect(sandboxEl.find('.dropdown-menu li').length).toBe(scope.icons.length); expect(sandboxEl.find('.dropdown-menu li:eq(0)').text().trim()).toBe(scope.icons[0].label); }); - + it('should support null ng-model initial value', function() { var elm = compileDirective('default', { selectedIcon: null }); expect(function() { angular.element(elm[0]).triggerHandler('focus') }).not.toThrow(); @@ -453,7 +457,20 @@ describe('select', function () { angular.element(sandboxEl.find('.dropdown-menu li:eq(3) a')[0]).triggerHandler('click'); angular.element(sandboxEl.find('.dropdown-menu li:eq(1) a')[0]).triggerHandler('click'); angular.element(sandboxEl.find('.dropdown-menu li:eq(2) a')[0]).triggerHandler('click'); - expect(scope.selectedIcons).toEqual([scope.icons[1].value, scope.icons[2].value, scope.icons[3].value]); + angular.element(sandboxEl.find('.dropdown-menu li:eq(7) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(6) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(4) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(9) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(5) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(10) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(8) a')[0]).triggerHandler('click'); + angular.element(sandboxEl.find('.dropdown-menu li:eq(11) a')[0]).triggerHandler('click'); + expect(scope.selectedIcons).toEqual([ + scope.icons[1].value, scope.icons[2].value, scope.icons[3].value, + scope.icons[4].value, scope.icons[5].value, scope.icons[6].value, + scope.icons[7].value, scope.icons[8].value, scope.icons[9].value, + scope.icons[10].value, scope.icons[11].value + ]); }); it('should sort selected items by selection order when sort it false', function() {