|
16 | 16 | $popup: false,
|
17 | 17 | $popupInput: false,
|
18 | 18 | $popupItems: false,
|
| 19 | + $popupCreate: false, |
19 | 20 | $popupHint: false,
|
20 | 21 |
|
21 | 22 | init: function($element, options) {
|
|
65 | 66 | .addClass('selectopus-popup-items')
|
66 | 67 | .appendTo(self.$popup);
|
67 | 68 |
|
| 69 | + self.$popupCreate = $('<li>') |
| 70 | + .addClass('selectopus-popup-item selectopus-popup-create') |
| 71 | + .text('create new item') |
| 72 | + .click(self.view.popup.items.onClick) |
| 73 | + .appendTo(self.$popupItems); |
| 74 | + |
68 | 75 | self.$popupHint = $('<div>')
|
69 | 76 | .addClass('selectopus-popup-hint')
|
70 | 77 | .appendTo(self.$popup);
|
|
119 | 126 | }
|
120 | 127 | });
|
121 | 128 |
|
122 |
| - var data_bool = ['multiple', 'popupHideSelected', 'popupSearchBar', 'popupSearchHideItem', 'popupSearchMarkItem', 'popupCloseAfterSelect']; |
| 129 | + var data_bool = ['multiple', 'allowCreate', 'popupHideSelected', 'popupSearchBar', 'popupSearchHideItem', 'popupSearchMarkItem', 'popupCloseAfterSelect']; |
123 | 130 |
|
124 | 131 | $.each(data_bool, function(idx, key) {
|
125 | 132 | var value = $element.data(key);
|
|
165 | 172 | });
|
166 | 173 | },
|
167 | 174 |
|
| 175 | + add: function(value, data) { |
| 176 | + self._options.items[value] = data; |
| 177 | + self._items[value] = data; |
| 178 | + }, |
| 179 | + |
168 | 180 | exists: function(value) {
|
169 | 181 | return typeof(self._items[value]) !== 'undefined';
|
170 | 182 | },
|
|
187 | 199 | keys: function() {
|
188 | 200 | var list = [];
|
189 | 201 |
|
190 |
| - $.each(self._value, function(value, title){ |
| 202 | + $.each(self._value, function(value){ |
191 | 203 | list.push(value);
|
192 | 204 | });
|
193 | 205 |
|
|
203 | 215 |
|
204 | 216 | self.$element.attr('multiple', self._options.multiple);
|
205 | 217 |
|
206 |
| - $.each(self._value, function(value, title) { |
| 218 | + $.each(self._value, function(value) { |
207 | 219 | $('<option>')
|
208 | 220 | .attr('selected', true)
|
209 | 221 | .text(value)
|
|
262 | 274 | },
|
263 | 275 |
|
264 | 276 | create: function(value) {
|
| 277 | + var data = self.value.get(value); |
| 278 | + |
265 | 279 | $('<span>')
|
266 | 280 | .addClass('selectopus-item')
|
267 |
| - .html(self._options.onRenderItem(self.public, value, self.value.get(value))) |
| 281 | + .html(self._options.onRenderItem(self.public, value, self._options.onGetLabel(self.public, data), data)) |
268 | 282 | .data('value', value)
|
269 | 283 | .click(self.view.items.onClick)
|
270 | 284 | .appendTo(self.$items);
|
|
303 | 317 |
|
304 | 318 | var items = {};
|
305 | 319 |
|
306 |
| - $.each(self._items, function(value, title){ |
| 320 | + $.each(self._items, function(value, data){ |
307 | 321 | var selected = self.value.exists(value);
|
308 | 322 |
|
309 | 323 | if (selected && (self._options.popupHideSelected)) {
|
310 | 324 | return;
|
311 | 325 | }
|
312 | 326 |
|
313 |
| - items[value] = title; |
| 327 | + items[value] = data; |
314 | 328 | });
|
315 | 329 |
|
316 | 330 | $.each(items, function(value) {
|
|
333 | 347 | },
|
334 | 348 |
|
335 | 349 | create: function(value) {
|
336 |
| - var content = self._options.onRenderPopupItem(self.public, value, self.items.get(value), self._search); |
| 350 | + var data = self.items.get(value); |
| 351 | + var label = self._options.onGetLabel(self.public, data); |
| 352 | + |
| 353 | + if (self._options.popupSearchMarkItem) { |
| 354 | + label = self.public.utils.mark(label, self._search); |
| 355 | + } |
| 356 | + |
| 357 | + var content = self._options.onRenderPopupItem(self.public, value, label, data); |
337 | 358 |
|
338 | 359 | var $item = $('<li>')
|
339 | 360 | .addClass('selectopus-popup-item')
|
|
348 | 369 | },
|
349 | 370 |
|
350 | 371 | clear: function() {
|
351 |
| - self.$popupItems.empty(); |
| 372 | + self.$popupItems.find('.selectopus-popup-item:not(.selectopus-popup-create)').remove(); |
352 | 373 | },
|
353 | 374 |
|
354 | 375 | onClick: function() {
|
355 | 376 | var value = $(this).data('value');
|
356 | 377 |
|
| 378 | + if ($(this).is('.selectopus-popup-create')) { |
| 379 | + self.items.add(value, self._options.onCreateItem(self.public, value)); |
| 380 | + } |
| 381 | + |
357 | 382 | if (self._options.multiple && self.value.exists(value)) {
|
358 | 383 | self.value.remove(value);
|
359 | 384 | self.value.save();
|
|
409 | 434 |
|
410 | 435 | self.$popupItems.scrollTop(0);
|
411 | 436 | self.$popupInput.val('').focus();
|
| 437 | + self.$popupCreate.hide(); |
412 | 438 | });
|
413 | 439 |
|
414 | 440 | },
|
|
470 | 496 | self.view.popup.items.createList();
|
471 | 497 | }
|
472 | 498 | });
|
| 499 | + |
| 500 | + if (self._options.allowCreate && (self._search.length > 0)) { |
| 501 | + var data = self._options.onCreateItem(self.public, self._search); |
| 502 | + var label = self._options.onGetLabel(self.public, data); |
| 503 | + |
| 504 | + if (self._options.popupSearchMarkItem) { |
| 505 | + label = self.public.utils.mark(label, self._search); |
| 506 | + } |
| 507 | + |
| 508 | + self.$popupCreate |
| 509 | + .data('value', self._search) |
| 510 | + .html(self._options.onRenderPopupItem(self.public, self._search, label, data)); |
| 511 | + |
| 512 | + self.$popupCreate.show(); |
| 513 | + } |
| 514 | + else { |
| 515 | + self.$popupCreate.hide(); |
| 516 | + } |
473 | 517 | }
|
474 | 518 | }
|
475 | 519 | },
|
|
589 | 633 | };
|
590 | 634 |
|
591 | 635 | $.fn.selectopus.default = {
|
592 |
| - items: {}, // List of allowed items {value1: title1, value2: title2} |
| 636 | + items: {}, // List of allowed items {value1: data1, value2: data2} |
593 | 637 | value: [], // List of values [value1, value2]
|
594 | 638 |
|
595 | 639 | language: 'en',
|
596 | 640 | multiple: false, // Allow multiple select
|
| 641 | + allowCreate: true, // Allow create new value |
597 | 642 |
|
598 | 643 | popupHideSelected: false, // Hide selected items (or only hightlite by default)
|
599 | 644 |
|
|
632 | 677 |
|
633 | 678 | var items = {};
|
634 | 679 |
|
635 |
| - $.each(selectopus.items, function(value, title) { |
636 |
| - if (!selectopus.utils.match(title, search)) { |
| 680 | + $.each(selectopus.items, function(value, data) { |
| 681 | + if (!selectopus.utils.match(selectopus.options.onGetLabel(selectopus, data), search)) { |
637 | 682 | return;
|
638 | 683 | }
|
639 | 684 |
|
640 |
| - items[value] = title; |
| 685 | + items[value] = data; |
641 | 686 | });
|
642 | 687 |
|
643 | 688 | return items;
|
644 | 689 | },
|
645 |
| - onRenderItem: function(selectopus, value, title) { |
646 |
| - return title; |
| 690 | + onGetLabel: function(selectopus, data) { |
| 691 | + return data; |
647 | 692 | },
|
648 |
| - onRenderPopupItem: function(selectopus, value, title, search) { |
649 |
| - if (selectopus.options.popupSearchMarkItem) { |
650 |
| - title = selectopus.utils.mark(title, search); |
651 |
| - } |
652 |
| - |
653 |
| - return title; |
| 693 | + onRenderItem: function(selectopus, value, label, data) { |
| 694 | + return label; |
| 695 | + }, |
| 696 | + onRenderPopupItem: function(selectopus, value, label, data) { |
| 697 | + return label; |
| 698 | + }, |
| 699 | + onCreateItem: function(selectopus, label) { |
| 700 | + return label; |
654 | 701 | }
|
655 | 702 | };
|
656 | 703 |
|
|
0 commit comments