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