|
1 | 1 | (function($) { |
2 | 2 | var originalTagit = $.fn.tagit; |
| 3 | + |
| 4 | + var updateAutocompleteWidth = function(el) { |
| 5 | + var $field = $(el); |
| 6 | + var $widget = $field |
| 7 | + .find('.tagit-new input[type="text"]') |
| 8 | + .autocomplete('widget'); |
| 9 | + var fieldOffset = $field.offset(); |
| 10 | + |
| 11 | + $widget |
| 12 | + .addClass('sori-interests-autocomplete') |
| 13 | + .css({ |
| 14 | + 'width': $field.outerWidth() + 'px', |
| 15 | + 'max-width': 'none', |
| 16 | + 'box-sizing': 'border-box', |
| 17 | + 'left': fieldOffset.left + 'px' |
| 18 | + }); |
| 19 | + }; |
| 20 | + |
| 21 | + var updateInterestsUi = function(el, placeholder) { |
| 22 | + var $field = $(el); |
| 23 | + var assignedTags = originalTagit.call(el, 'assignedTags') || []; |
| 24 | + var isEmpty = assignedTags.length === 0; |
| 25 | + var $input = $field.find('.tagit-new input[type="text"]'); |
| 26 | + |
| 27 | + $field.addClass('sori-interests-selectLike'); |
| 28 | + $field.toggleClass('sori-interests-empty', isEmpty); |
| 29 | + $input.attr('placeholder', isEmpty ? placeholder : ''); |
| 30 | + }; |
| 31 | + |
3 | 32 | $.fn.tagit = function(method) { |
4 | | - var result = originalTagit.apply(this, arguments); |
5 | 33 | if (typeof method !== 'string' |
6 | 34 | && $(this).hasClass('interests') |
7 | 35 | && !$(this).data('soriReinit')) { |
8 | 36 | var ns = $.pkp && $.pkp.plugins |
9 | 37 | && $.pkp.plugins.generic |
10 | 38 | && $.pkp.plugins.generic.selectionOfReviewingInterests; |
11 | 39 | if (ns && ns.interestsOptions) { |
12 | | - var existingTags = originalTagit.call(this, 'assignedTags') || []; |
13 | | - var existingTagsLower = $.map(existingTags, function(tag) { |
14 | | - return tag.toLowerCase(); |
15 | | - }); |
16 | | - |
17 | | - originalTagit.call(this, 'destroy'); |
18 | 40 | var el = this; |
19 | | - originalTagit.call(this, { |
| 41 | + var placeholder = ns.placeholder || 'Selecione uma ou mais opcoes'; |
| 42 | + var baseOptions = method || {}; |
| 43 | + var preExistingTagsLower = $.map($(this).children('li:not(.tagit-new)'), function(tag) { |
| 44 | + return $.trim($(tag).text()).toLowerCase(); |
| 45 | + }); |
| 46 | + var originalBeforeTagAdded = baseOptions.beforeTagAdded; |
| 47 | + var originalAfterTagAdded = baseOptions.afterTagAdded; |
| 48 | + var originalAfterTagRemoved = baseOptions.afterTagRemoved; |
| 49 | + var configuredOptions = $.extend(true, {}, baseOptions, { |
20 | 50 | fieldName: 'interests[]', |
21 | | - availableTags: ns.interestsOptions, |
22 | 51 | allowSpaces: true, |
23 | | - autocomplete: {delay: 0, minLength: 0}, |
| 52 | + showAutocompleteOnFocus: true, |
| 53 | + autocomplete: $.extend({}, baseOptions.autocomplete || {}, { |
| 54 | + delay: 0, |
| 55 | + minLength: 0, |
| 56 | + source: function(search, showChoices) { |
| 57 | + var filter = search.term.toLowerCase(); |
| 58 | + var choices = $.grep(ns.interestsOptions, function(option) { |
| 59 | + return option.toLowerCase().indexOf(filter) === 0; |
| 60 | + }); |
| 61 | + if (!this.options.allowDuplicates) { |
| 62 | + choices = this._subtractArray(choices, this.assignedTags()); |
| 63 | + } |
| 64 | + showChoices(choices); |
| 65 | + } |
| 66 | + }), |
24 | 67 | beforeTagAdded: function(event, ui) { |
25 | | - var availableTags = originalTagit.call(el, 'option', 'availableTags'); |
26 | 68 | var tagLower = ui.tagLabel.toLowerCase(); |
27 | | - var inAllowedList = $.map(availableTags, function(tag) { |
| 69 | + var normalizedAllowedList = $.map(ns.interestsOptions, function(tag) { |
28 | 70 | return tag.toLowerCase(); |
29 | | - }).indexOf(tagLower) !== -1; |
30 | | - var isPreExisting = existingTagsLower.indexOf(tagLower) !== -1; |
31 | | - return inAllowedList || isPreExisting; |
32 | | - } |
33 | | - }); |
| 71 | + }); |
| 72 | + var inAllowedList = normalizedAllowedList.indexOf(tagLower) !== -1; |
| 73 | + var isPreExisting = preExistingTagsLower.indexOf(tagLower) !== -1; |
34 | 74 |
|
35 | | - $.each(existingTags, function(i, tag) { |
36 | | - var currentTags = originalTagit.call(el, 'assignedTags') || []; |
37 | | - var currentTagsLower = $.map(currentTags, function(t) { |
38 | | - return t.toLowerCase(); |
39 | | - }); |
40 | | - if (currentTagsLower.indexOf(tag.toLowerCase()) === -1) { |
41 | | - originalTagit.call(el, 'createTag', tag); |
| 75 | + if (!inAllowedList && !isPreExisting) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + if (typeof originalBeforeTagAdded === 'function') { |
| 80 | + return originalBeforeTagAdded.call(this, event, ui); |
| 81 | + } |
| 82 | + }, |
| 83 | + afterTagAdded: function(event, ui) { |
| 84 | + $(el).find('.tagit-new input[type="text"]').autocomplete(); |
| 85 | + updateInterestsUi(el, placeholder); |
| 86 | + if (typeof originalAfterTagAdded === 'function') { |
| 87 | + originalAfterTagAdded.call(this, event, ui); |
| 88 | + } |
| 89 | + }, |
| 90 | + afterTagRemoved: function(event, ui) { |
| 91 | + $(el).find('.tagit-new input[type="text"]').autocomplete(); |
| 92 | + updateInterestsUi(el, placeholder); |
| 93 | + if (typeof originalAfterTagRemoved === 'function') { |
| 94 | + originalAfterTagRemoved.call(this, event, ui); |
| 95 | + } |
42 | 96 | } |
43 | 97 | }); |
44 | | - $(this).data('soriReinit', true); |
45 | | - $(document) |
46 | | - .off('focus.sori click.sori', '.tagit-new input') |
47 | | - .on('focus.sori click.sori', '.tagit-new input', function() { |
| 98 | + var result = originalTagit.call(this, configuredOptions); |
| 99 | + var $input = $(this).find('.tagit-new input[type="text"]'); |
| 100 | + |
| 101 | + updateInterestsUi(el, placeholder); |
| 102 | + $input |
| 103 | + .off('autocompleteopen.sori click.sori') |
| 104 | + .on('autocompleteopen.sori', function() { |
| 105 | + updateAutocompleteWidth(el); |
| 106 | + }) |
| 107 | + .on('click.sori', function() { |
48 | 108 | $(this).autocomplete('search', ''); |
49 | 109 | }); |
| 110 | + $(this).data('soriReinit', true); |
| 111 | + |
| 112 | + return result; |
50 | 113 | } |
51 | 114 | } |
52 | | - return result; |
| 115 | + |
| 116 | + return originalTagit.apply(this, arguments); |
53 | 117 | }; |
54 | 118 | })(jQuery); |
0 commit comments