1
1
/*!
2
2
* ui-select
3
3
* http://github.com/angular-ui/ui-select
4
- * Version: 0.12.1 - 2015-08-05T18:50:43.112Z
4
+ * Version: 0.12.1 - 2015-08-06T11:06:02.062Z
5
5
* License: MIT
6
6
*/
7
7
8
8
9
+ ( function ( ) {
10
+ "use strict" ;
11
+ // Make multiple matches sortable
12
+ angular . module ( 'ui.select.sort' , [ 'ui.select' ] )
13
+ . directive ( 'uiSelectSort' ,
14
+ [ '$timeout' , 'uiSelectConfig' , 'uiSelectMinErr' , function ( $timeout , uiSelectConfig , uiSelectMinErr ) {
15
+ return {
16
+ require : '^uiSelect' ,
17
+ link : function ( scope , element , attrs , $select ) {
18
+ if ( scope [ attrs . uiSelectSort ] === null ) {
19
+ throw uiSelectMinErr ( 'sort' , "Expected a list to sort" ) ;
20
+ }
21
+
22
+ var options = angular . extend ( {
23
+ axis : 'horizontal'
24
+ } ,
25
+ scope . $eval ( attrs . uiSelectSortOptions ) ) ;
26
+
27
+ var axis = options . axis ,
28
+ draggingClassName = 'dragging' ,
29
+ droppingClassName = 'dropping' ,
30
+ droppingBeforeClassName = 'dropping-before' ,
31
+ droppingAfterClassName = 'dropping-after' ;
32
+
33
+ scope . $watch ( function ( ) {
34
+ return $select . sortable ;
35
+ } , function ( n ) {
36
+ if ( n ) {
37
+ element . attr ( 'draggable' , true ) ;
38
+ } else {
39
+ element . removeAttr ( 'draggable' ) ;
40
+ }
41
+ } ) ;
42
+
43
+ element . on ( 'dragstart' , function ( e ) {
44
+ element . addClass ( draggingClassName ) ;
45
+
46
+ ( e . dataTransfer || e . originalEvent . dataTransfer ) . setData ( 'text/plain' , scope . $index ) ;
47
+ } ) ;
48
+
49
+ element . on ( 'dragend' , function ( ) {
50
+ element . removeClass ( draggingClassName ) ;
51
+ } ) ;
52
+
53
+ var move = function ( from , to ) {
54
+ /*jshint validthis: true */
55
+ this . splice ( to , 0 , this . splice ( from , 1 ) [ 0 ] ) ;
56
+ } ;
57
+
58
+ var dragOverHandler = function ( e ) {
59
+ e . preventDefault ( ) ;
60
+
61
+ var offset = axis === 'vertical' ?
62
+ e . offsetY || e . layerY || ( e . originalEvent ? e . originalEvent . offsetY : 0 ) :
63
+ e . offsetX || e . layerX || ( e . originalEvent ? e . originalEvent . offsetX : 0 ) ;
64
+
65
+ if ( offset < ( this [ axis === 'vertical' ? 'offsetHeight' : 'offsetWidth' ] / 2 ) ) {
66
+ element . removeClass ( droppingAfterClassName ) ;
67
+ element . addClass ( droppingBeforeClassName ) ;
68
+
69
+ } else {
70
+ element . removeClass ( droppingBeforeClassName ) ;
71
+ element . addClass ( droppingAfterClassName ) ;
72
+ }
73
+ } ;
74
+
75
+ var dropTimeout ;
76
+
77
+ var dropHandler = function ( e ) {
78
+ e . preventDefault ( ) ;
79
+
80
+ var droppedItemIndex = parseInt ( ( e . dataTransfer ||
81
+ e . originalEvent . dataTransfer ) . getData ( 'text/plain' ) , 10 ) ;
82
+
83
+ // prevent event firing multiple times in firefox
84
+ $timeout . cancel ( dropTimeout ) ;
85
+ dropTimeout = $timeout ( function ( ) {
86
+ _dropHandler ( droppedItemIndex ) ;
87
+ } , 20 ) ;
88
+ } ;
89
+
90
+ var _dropHandler = function ( droppedItemIndex ) {
91
+ var theList = scope . $eval ( attrs . uiSelectSort ) ,
92
+ itemToMove = theList [ droppedItemIndex ] ,
93
+ newIndex = null ;
94
+
95
+ if ( element . hasClass ( droppingBeforeClassName ) ) {
96
+ if ( droppedItemIndex < scope . $index ) {
97
+ newIndex = scope . $index - 1 ;
98
+ } else {
99
+ newIndex = scope . $index ;
100
+ }
101
+ } else {
102
+ if ( droppedItemIndex < scope . $index ) {
103
+ newIndex = scope . $index ;
104
+ } else {
105
+ newIndex = scope . $index + 1 ;
106
+ }
107
+ }
108
+
109
+ move . apply ( theList , [ droppedItemIndex , newIndex ] ) ;
110
+
111
+ scope . $apply ( function ( ) {
112
+ scope . $emit ( 'uiSelectSort:change' , {
113
+ array : theList ,
114
+ item : itemToMove ,
115
+ from : droppedItemIndex ,
116
+ to : newIndex
117
+ } ) ;
118
+ } ) ;
119
+
120
+ element . removeClass ( droppingClassName ) ;
121
+ element . removeClass ( droppingBeforeClassName ) ;
122
+ element . removeClass ( droppingAfterClassName ) ;
123
+
124
+ element . off ( 'drop' , dropHandler ) ;
125
+ } ;
126
+
127
+ element . on ( 'dragenter' , function ( ) {
128
+ if ( element . hasClass ( draggingClassName ) ) {
129
+ return ;
130
+ }
131
+
132
+ element . addClass ( droppingClassName ) ;
133
+
134
+ element . on ( 'dragover' , dragOverHandler ) ;
135
+ element . on ( 'drop' , dropHandler ) ;
136
+ } ) ;
137
+
138
+ element . on ( 'dragleave' , function ( e ) {
139
+ if ( e . target != element ) {
140
+ return ;
141
+ }
142
+ element . removeClass ( droppingClassName ) ;
143
+ element . removeClass ( droppingBeforeClassName ) ;
144
+ element . removeClass ( droppingAfterClassName ) ;
145
+
146
+ element . off ( 'dragover' , dragOverHandler ) ;
147
+ element . off ( 'drop' , dropHandler ) ;
148
+ } ) ;
149
+ }
150
+ } ;
151
+ } ] ) ;
152
+
153
+ } ( ) ) ;
9
154
( function ( ) {
10
155
"use strict" ;
11
156
var KEY = {
@@ -588,7 +733,6 @@ uis.controller('uiSelectCtrl',
588
733
* @return {boolean } true if the item is disabled
589
734
*/
590
735
ctrl . isDisabled = function ( itemScope ) {
591
-
592
736
if ( ! ctrl . open ) {
593
737
return false ;
594
738
}
@@ -608,7 +752,8 @@ uis.controller('uiSelectCtrl',
608
752
609
753
610
754
/**
611
- * Selects an item
755
+ * Selects an item. Calls the onBeforeSelect and onSelect callbacks
756
+ * onBeforeSelect can alter or abort the selection
612
757
*
613
758
* Called when the user selects an item with ENTER or clicks the dropdown
614
759
*/
@@ -648,6 +793,12 @@ uis.controller('uiSelectCtrl',
648
793
}
649
794
} ;
650
795
796
+ // If there's no onBeforeSelect callback, then just call the completeCallback
797
+ if ( ! angular . isDefined ( ctrl . onBeforeRemoveCallback ) ) {
798
+ completeCallback ( item ) ;
799
+ return ;
800
+ }
801
+
651
802
// Call the onBeforeSelect callback
652
803
// Allowable responses are -:
653
804
// falsy: Abort the selection
@@ -658,11 +809,15 @@ uis.controller('uiSelectCtrl',
658
809
if ( angular . isDefined ( result ) ) {
659
810
if ( angular . isFunction ( result . then ) ) {
660
811
// Promise returned - wait for it to complete before completing the selection
661
- result . then ( function ( result ) {
662
- if ( ! result ) {
812
+ result . then ( function ( response ) {
813
+ if ( ! response ) {
663
814
return ;
664
815
}
665
- completeCallback ( result ) ;
816
+ if ( response === true ) {
817
+ completeCallback ( item ) ;
818
+ } else if ( response ) {
819
+ completeCallback ( response ) ;
820
+ }
666
821
} ) ;
667
822
} else if ( result === true ) {
668
823
completeCallback ( item ) ;
@@ -914,8 +1069,8 @@ uis.controller('uiSelectCtrl',
914
1069
} ] ) ;
915
1070
916
1071
uis . directive ( 'uiSelect' ,
917
- [ '$document' , 'uiSelectConfig' , 'uiSelectMinErr' , 'uisOffset' , '$compile' , '$parse' , '$timeout' ,
918
- function ( $document , uiSelectConfig , uiSelectMinErr , uisOffset , $compile , $parse , $timeout ) {
1072
+ [ '$document' , '$window' , ' uiSelectConfig', 'uiSelectMinErr' , 'uisOffset' , '$compile' , '$parse' , '$timeout' ,
1073
+ function ( $document , $window , uiSelectConfig , uiSelectMinErr , uisOffset , $compile , $parse , $timeout ) {
919
1074
920
1075
return {
921
1076
restrict : 'EA' ,
@@ -1162,9 +1317,7 @@ uis.directive('uiSelect',
1162
1317
var offsetDropdown = uisOffset ( dropdown ) ;
1163
1318
1164
1319
// Determine if the direction of the dropdown needs to be changed.
1165
- if ( offset . top + offset . height + offsetDropdown . height >
1166
- $document [ 0 ] . documentElement . scrollTop +
1167
- $document [ 0 ] . documentElement . clientHeight ) {
1320
+ if ( offset . top + offset . height + offsetDropdown . height > $window . pageYOffset + $document [ 0 ] . documentElement . clientHeight ) {
1168
1321
element . addClass ( directionUpClassName ) ;
1169
1322
}
1170
1323
@@ -1263,11 +1416,6 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr', '$timeout', function (uiSel
1263
1416
ctrl . activeMatchIndex = - 1 ;
1264
1417
$select . sizeSearchInput ( ) ;
1265
1418
1266
- // If there's no onBeforeRemove callback, then we're done
1267
- if ( ! angular . isDefined ( ctrl . onBeforeRemoveCallback ) ) {
1268
- return ;
1269
- }
1270
-
1271
1419
var callbackContext = {
1272
1420
$item : removedChoice ,
1273
1421
$model : $select . parserResult . modelMapper ( $scope , locals )
@@ -1282,6 +1430,12 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr', '$timeout', function (uiSel
1282
1430
ctrl . updateModel ( ) ;
1283
1431
}
1284
1432
1433
+ // If there's no onBeforeRemove callback, then just call the completeCallback
1434
+ if ( ! angular . isDefined ( ctrl . onBeforeRemoveCallback ) ) {
1435
+ completeCallback ( ) ;
1436
+ return ;
1437
+ }
1438
+
1285
1439
// Call the onBeforeRemove callback
1286
1440
// Allowable responses are -:
1287
1441
// falsy: Abort the removal
0 commit comments