@@ -22,105 +22,59 @@ Selectize.define('remove_button', function(options) {
22
22
append : true
23
23
} , options ) ;
24
24
25
- var singleClose = function ( thisRef , options ) {
26
-
25
+ var self = this ;
26
+ if ( this . settings . mode === 'single' ) {
27
27
options . className = options . className || 'remove-single' ;
28
+ }
28
29
29
- var self = thisRef ;
30
- var html = '<a href="javascript:void(0)" class="' + options . className + '" tabindex="-1" title="' + escape_html ( options . title ) + '">' + options . label + '</a>' ;
31
-
32
- /**
33
- * Appends an element as a child (with raw HTML).
34
- *
35
- * @param {string } html_container
36
- * @param {string } html_element
37
- * @return {string }
38
- */
39
- var append = function ( html_container , html_element ) {
40
- var pos = html_container . search ( / ( < \/ [ ^ > ] + > \s * ) $ / ) ;
41
- return html_container . substring ( 0 , pos ) + html_element + html_container . substring ( pos ) ;
42
- } ;
43
-
44
- thisRef . setup = ( function ( ) {
45
- var original = self . setup ;
46
- return function ( ) {
47
- // override the item rendering method to add the button to each
48
- if ( options . append ) {
49
- var id = $ ( self . $input . context ) . attr ( 'id' ) ;
50
- var selectizer = $ ( '#' + id ) ;
51
-
52
- var render_item = self . settings . render . item ;
53
- self . settings . render . item = function ( data ) {
54
- return append ( render_item . apply ( thisRef , arguments ) , html ) ;
55
- } ;
56
- }
57
-
58
- original . apply ( thisRef , arguments ) ;
59
-
60
- // add event listener
61
- thisRef . $control . on ( 'click' , '.' + options . className , function ( e ) {
62
- e . preventDefault ( ) ;
63
- if ( self . isLocked ) return ;
64
-
65
- // use removeItem - clear() will not trigger the 'item_remove' event
66
- var $item = $ ( e . currentTarget ) . parent ( ) ;
67
- self . removeItem ( $item ) ;
68
- } ) ;
69
-
70
- } ;
71
- } ) ( ) ;
30
+ var html = '<a href="javascript:void(0)" class="' + options . className + '" tabindex="-1" title="' + escape_html ( options . title ) + '">' + options . label + '</a>' ;
31
+
32
+ /**
33
+ * Appends an element as a child (with raw HTML).
34
+ *
35
+ * @param {string } html_container
36
+ * @param {string } html_element
37
+ * @return {string }
38
+ */
39
+ var append = function ( html_container , html_element ) {
40
+ var pos = html_container . search ( / ( < \/ [ ^ > ] + > \s * ) $ / ) ;
41
+ return html_container . substring ( 0 , pos ) + html_element + html_container . substring ( pos ) ;
72
42
} ;
73
43
74
- var multiClose = function ( thisRef , options ) {
75
-
76
- var self = thisRef ;
77
- var html = '<a href="javascript:void(0)" class="' + options . className + '" tabindex="-1" title="' + escape_html ( options . title ) + '">' + options . label + '</a>' ;
78
-
79
- /**
80
- * Appends an element as a child (with raw HTML).
81
- *
82
- * @param {string } html_container
83
- * @param {string } html_element
84
- * @return {string }
85
- */
86
- var append = function ( html_container , html_element ) {
87
- var pos = html_container . search ( / ( < \/ [ ^ > ] + > \s * ) $ / ) ;
88
- return html_container . substring ( 0 , pos ) + html_element + html_container . substring ( pos ) ;
89
- } ;
90
-
91
- thisRef . setup = ( function ( ) {
92
- var original = self . setup ;
93
- return function ( ) {
94
- // override the item rendering method to add the button to each
95
- if ( options . append ) {
96
- var render_item = self . settings . render . item ;
97
- self . settings . render . item = function ( data ) {
98
- return append ( render_item . apply ( thisRef , arguments ) , html ) ;
99
- } ;
100
- }
101
-
102
- original . apply ( thisRef , arguments ) ;
103
-
104
- // add event listener
105
- thisRef . $control . on ( 'click' , '.' + options . className , function ( e ) {
106
- e . preventDefault ( ) ;
107
- if ( self . isLocked ) return ;
108
-
109
- var $item = $ ( e . currentTarget ) . parent ( ) ;
44
+ self . setup = ( function ( ) {
45
+ var original = self . setup ;
46
+ return function ( ) {
47
+ // override the item rendering method to add the button to each
48
+ if ( options . append ) {
49
+ var render_item = self . settings . render . item ;
50
+ self . settings . render . item = function ( data ) {
51
+ return append ( render_item . apply ( self , arguments ) , html ) ;
52
+ } ;
53
+ }
54
+
55
+ original . apply ( self , arguments ) ;
56
+
57
+ // add event listener
58
+ self . $control . on ( 'click' , '.' + options . className , function ( e ) {
59
+ e . preventDefault ( ) ;
60
+ if ( self . isLocked ) return ;
61
+
62
+ // use deleteSelection()
63
+ // clear() will not trigger the 'item_remove' event
64
+ // removeItem() will not trigger the 'onDelete' callback
65
+ var $item = $ ( e . currentTarget ) . parent ( ) ;
66
+
67
+ if ( self . settings . mode === 'single' ) {
68
+ self . setCaret ( ) ; // 1 for 'single'
69
+ e [ 'keyCode' ] = KEY_BACKSPACE ; // makes deleteSelection calculate the correct direction
70
+ } else {
110
71
self . setActiveItem ( $item ) ;
111
- if ( self . deleteSelection ( ) ) {
112
- self . setCaret ( self . items . length ) ;
113
- }
114
- } ) ;
115
-
116
- } ;
117
- } ) ( ) ;
118
- } ;
72
+ }
73
+ if ( self . deleteSelection ( e ) ) {
74
+ self . setCaret ( self . items . length ) ;
75
+ }
76
+ } ) ;
119
77
120
- if ( this . settings . mode === 'single' ) {
121
- singleClose ( this , options ) ;
122
- return ;
123
- } else {
124
- multiClose ( this , options ) ;
125
- }
78
+ } ;
79
+ } ) ( ) ;
126
80
} ) ;
0 commit comments