|
146 | 146 | inputClass: 'selectize-input',
|
147 | 147 | dropdownClass: 'selectize-dropdown',
|
148 | 148 |
|
| 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 | + |
149 | 159 | render: {
|
150 | 160 | item: null,
|
151 | 161 | option: null,
|
|
542 | 552 | this.isSetup = true;
|
543 | 553 | };
|
544 | 554 |
|
| 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 | + |
545 | 569 | /**
|
546 | 570 | * Triggered on <input> keypress.
|
547 | 571 | *
|
|
632 | 656 | if (this.lastValue !== value) {
|
633 | 657 | this.lastValue = value;
|
634 | 658 | this.refreshOptions();
|
| 659 | + this.trigger('onType', value); |
635 | 660 | }
|
636 | 661 | };
|
637 | 662 |
|
|
1127 | 1152 | this.userOptions[value] = true;
|
1128 | 1153 | this.options[value] = data;
|
1129 | 1154 | this.lastQuery = null;
|
| 1155 | + this.trigger('onOptionAdd', value, data); |
1130 | 1156 | };
|
1131 | 1157 |
|
1132 | 1158 | /**
|
|
1165 | 1191 | delete this.userOptions[value];
|
1166 | 1192 | delete this.options[value];
|
1167 | 1193 | this.lastQuery = null;
|
| 1194 | + this.trigger('onOptionRemove', value); |
1168 | 1195 | };
|
1169 | 1196 |
|
1170 | 1197 | /**
|
|
1204 | 1231 | * @param {string} value
|
1205 | 1232 | */
|
1206 | 1233 | Selectize.prototype.addItem = function(value) {
|
| 1234 | + var $item; |
1207 | 1235 | var inputMode = this.settings.mode;
|
1208 | 1236 | var isFull = this.isFull();
|
1209 | 1237 | value = String(value);
|
|
1213 | 1241 | if (this.items.indexOf(value) !== -1) return;
|
1214 | 1242 | if (!this.options.hasOwnProperty(value)) return;
|
1215 | 1243 |
|
| 1244 | + $item = $(this.render('item', this.options[value])); |
1216 | 1245 | this.items.splice(this.caretPos, 0, value);
|
1217 |
| - this.insertAtCaret(this.render('item', this.options[value])); |
| 1246 | + this.insertAtCaret($item); |
1218 | 1247 |
|
1219 | 1248 | isFull = this.isFull();
|
1220 | 1249 | this.$control.toggleClass('has-items', true);
|
|
1247 | 1276 | }
|
1248 | 1277 |
|
1249 | 1278 | this.updateOriginalInput();
|
| 1279 | + this.trigger('onItemAdd', value, $item); |
1250 | 1280 | }
|
1251 | 1281 | };
|
1252 | 1282 |
|
|
1286 | 1316 |
|
1287 | 1317 | this.updatePlaceholder();
|
1288 | 1318 | this.updateOriginalInput();
|
| 1319 | + this.trigger('onItemRemove', value); |
1289 | 1320 | }
|
1290 | 1321 | };
|
1291 | 1322 |
|
|
1379 | 1410 | } else {
|
1380 | 1411 | this.$input.val(this.getValue());
|
1381 | 1412 | }
|
| 1413 | + |
1382 | 1414 | this.$input.trigger('change');
|
| 1415 | + if (this.isSetup) { |
| 1416 | + this.trigger('onChange', this.$input.val()); |
| 1417 | + } |
1383 | 1418 | };
|
1384 | 1419 |
|
1385 | 1420 | /**
|
|
1408 | 1443 | this.positionDropdown();
|
1409 | 1444 | this.$control.addClass('dropdown-active');
|
1410 | 1445 | this.$dropdown.show();
|
| 1446 | + this.trigger('onDropdownOpen', this.$dropdown); |
1411 | 1447 | };
|
1412 | 1448 |
|
1413 | 1449 | /**
|
1414 | 1450 | * Closes the autocomplete dropdown menu.
|
1415 | 1451 | */
|
1416 | 1452 | Selectize.prototype.close = function() {
|
| 1453 | + if (!this.isOpen) return; |
1417 | 1454 | this.$dropdown.hide();
|
1418 | 1455 | this.$control.removeClass('dropdown-active');
|
1419 | 1456 | this.isOpen = false;
|
| 1457 | + this.trigger('onDropdownClose', this.$dropdown); |
1420 | 1458 | };
|
1421 | 1459 |
|
1422 | 1460 | /**
|
|
1447 | 1485 | this.setCaret(0);
|
1448 | 1486 | this.updatePlaceholder();
|
1449 | 1487 | this.updateOriginalInput();
|
| 1488 | + this.trigger('onClear'); |
1450 | 1489 | };
|
1451 | 1490 |
|
1452 | 1491 | /**
|
|
0 commit comments