Skip to content

Commit 28da826

Browse files
committed
Added event callbacks.
1 parent 2df88bc commit 28da826

File tree

4 files changed

+119
-40
lines changed

4 files changed

+119
-40
lines changed

jquery.selectize.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@
146146
inputClass: 'selectize-input',
147147
dropdownClass: 'selectize-dropdown',
148148

149+
onChange : null, // function(value)
150+
onItemAdd : null, // function(value, $item) { ... }
151+
onItemRemove : null, // function(value) { ... }
152+
onClear : null, // function() { ... }
153+
onOptionAdd : null, // function(value, data) { ... }
154+
onOptionRemove : null, // function(value) { ... }
155+
onDropdownOpen : null, // function($dropdown) { ... }
156+
onDropdownClose : null, // function($dropdown) { ... }
157+
onType : null, // function(str) { ... }
158+
149159
render: {
150160
item: null,
151161
option: null,
@@ -542,6 +552,20 @@
542552
this.isSetup = true;
543553
};
544554

555+
/**
556+
* Triggers a callback defined in the user-provided settings.
557+
* Events: onItemAdd, onOptionAdd, etc
558+
*
559+
* @param {string} event
560+
*/
561+
Selectize.prototype.trigger = function(event) {
562+
var args;
563+
if (typeof this.settings[event] === 'function') {
564+
args = Array.prototype.slice.apply(arguments, [1]);
565+
this.settings.event.apply(this, args);
566+
}
567+
};
568+
545569
/**
546570
* Triggered on <input> keypress.
547571
*
@@ -632,6 +656,7 @@
632656
if (this.lastValue !== value) {
633657
this.lastValue = value;
634658
this.refreshOptions();
659+
this.trigger('onType', value);
635660
}
636661
};
637662

@@ -1127,6 +1152,7 @@
11271152
this.userOptions[value] = true;
11281153
this.options[value] = data;
11291154
this.lastQuery = null;
1155+
this.trigger('onOptionAdd', value, data);
11301156
};
11311157

11321158
/**
@@ -1165,6 +1191,7 @@
11651191
delete this.userOptions[value];
11661192
delete this.options[value];
11671193
this.lastQuery = null;
1194+
this.trigger('onOptionRemove', value);
11681195
};
11691196

11701197
/**
@@ -1204,6 +1231,7 @@
12041231
* @param {string} value
12051232
*/
12061233
Selectize.prototype.addItem = function(value) {
1234+
var $item;
12071235
var inputMode = this.settings.mode;
12081236
var isFull = this.isFull();
12091237
value = String(value);
@@ -1213,8 +1241,9 @@
12131241
if (this.items.indexOf(value) !== -1) return;
12141242
if (!this.options.hasOwnProperty(value)) return;
12151243

1244+
$item = $(this.render('item', this.options[value]));
12161245
this.items.splice(this.caretPos, 0, value);
1217-
this.insertAtCaret(this.render('item', this.options[value]));
1246+
this.insertAtCaret($item);
12181247

12191248
isFull = this.isFull();
12201249
this.$control.toggleClass('has-items', true);
@@ -1247,6 +1276,7 @@
12471276
}
12481277

12491278
this.updateOriginalInput();
1279+
this.trigger('onItemAdd', value, $item);
12501280
}
12511281
};
12521282

@@ -1286,6 +1316,7 @@
12861316

12871317
this.updatePlaceholder();
12881318
this.updateOriginalInput();
1319+
this.trigger('onItemRemove', value);
12891320
}
12901321
};
12911322

@@ -1379,7 +1410,11 @@
13791410
} else {
13801411
this.$input.val(this.getValue());
13811412
}
1413+
13821414
this.$input.trigger('change');
1415+
if (this.isSetup) {
1416+
this.trigger('onChange', this.$input.val());
1417+
}
13831418
};
13841419

13851420
/**
@@ -1408,15 +1443,18 @@
14081443
this.positionDropdown();
14091444
this.$control.addClass('dropdown-active');
14101445
this.$dropdown.show();
1446+
this.trigger('onDropdownOpen', this.$dropdown);
14111447
};
14121448

14131449
/**
14141450
* Closes the autocomplete dropdown menu.
14151451
*/
14161452
Selectize.prototype.close = function() {
1453+
if (!this.isOpen) return;
14171454
this.$dropdown.hide();
14181455
this.$control.removeClass('dropdown-active');
14191456
this.isOpen = false;
1457+
this.trigger('onDropdownClose', this.$dropdown);
14201458
};
14211459

14221460
/**
@@ -1447,6 +1485,7 @@
14471485
this.setCaret(0);
14481486
this.updatePlaceholder();
14491487
this.updateOriginalInput();
1488+
this.trigger('onClear');
14501489
};
14511490

14521491
/**

0 commit comments

Comments
 (0)