2015-04-14 03:15:28 +02:00
|
|
|
/**
|
|
|
|
* Register TagField functions with fields.
|
|
|
|
*/
|
|
|
|
(function ($) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The multiple-select plugin (Chosen) applies itself to everything!
|
|
|
|
* We have to remove it before selectively applying select2, or
|
|
|
|
* we'll see an extra field where there should be only one.
|
|
|
|
*/
|
|
|
|
$.fn.chosenDestroy = function () {
|
2015-06-19 17:44:35 +02:00
|
|
|
var $this = $(this);
|
2017-01-13 20:11:59 +01:00
|
|
|
if ($this.siblings('.chosen-container').length) {
|
2015-06-19 17:44:35 +02:00
|
|
|
$this
|
|
|
|
.show() // The field needs to be visible so Select2 evaluates the width correctly.
|
2017-01-13 20:11:59 +01:00
|
|
|
.removeClass('chosen-done')
|
|
|
|
.removeClass('has-chosen')
|
2015-06-19 17:44:35 +02:00
|
|
|
.next()
|
|
|
|
.remove();
|
|
|
|
}
|
|
|
|
return $this;
|
2015-04-14 03:15:28 +02:00
|
|
|
};
|
|
|
|
|
2015-04-23 21:19:10 +02:00
|
|
|
$.entwine('ss', function ($) {
|
2015-04-14 03:15:28 +02:00
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
$('.ss-tag-field.has-chosen + .chosen-container, .ss-tag-field:not(.has-chosen)').entwine({
|
2015-04-21 23:18:59 +02:00
|
|
|
applySelect2: function () {
|
|
|
|
var self = this,
|
2015-06-19 17:44:35 +02:00
|
|
|
$select = $(this);
|
|
|
|
|
|
|
|
if ($select.prev().hasClass('ss-tag-field')) {
|
|
|
|
$select = $select.prev();
|
|
|
|
}
|
2015-04-21 23:18:59 +02:00
|
|
|
|
|
|
|
// There is a race condition where Select2 might not
|
|
|
|
// be bound to jQuery yet. So here we make sure Select2
|
|
|
|
// is defined before trying to invoke it.
|
|
|
|
if ($.fn.select2 === void 0) {
|
|
|
|
return setTimeout(function () {
|
|
|
|
self.applySelect2();
|
|
|
|
}, 0);
|
|
|
|
}
|
2015-04-21 02:46:41 +02:00
|
|
|
|
2015-04-23 21:19:10 +02:00
|
|
|
var options = {
|
|
|
|
'tags': true,
|
2018-02-20 14:39:58 +01:00
|
|
|
'width': 'resolve',
|
2015-05-28 11:36:06 +02:00
|
|
|
'tokenSeparators': [',']
|
2015-04-23 21:19:10 +02:00
|
|
|
};
|
|
|
|
|
2016-07-21 22:52:17 +02:00
|
|
|
if ($select.attr('data-can-create') == 0) {
|
|
|
|
options.tags = false;
|
|
|
|
}
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if ($select.attr('data-ss-tag-field-suggest-url')) {
|
2015-04-23 21:19:10 +02:00
|
|
|
options.ajax = {
|
2015-05-15 12:29:17 +02:00
|
|
|
'url': $select.attr('data-ss-tag-field-suggest-url'),
|
2015-04-23 21:19:10 +02:00
|
|
|
'dataType': 'json',
|
|
|
|
'delay': 250,
|
|
|
|
'data': function (params) {
|
|
|
|
return {
|
|
|
|
'term': params.term
|
|
|
|
};
|
|
|
|
},
|
|
|
|
'processResults': function (data) {
|
|
|
|
return {
|
|
|
|
'results': data.items
|
|
|
|
};
|
|
|
|
},
|
|
|
|
'cache': true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 02:46:41 +02:00
|
|
|
$select
|
|
|
|
.chosenDestroy()
|
2015-04-23 21:19:10 +02:00
|
|
|
.select2(options);
|
2015-04-21 23:18:59 +02:00
|
|
|
},
|
2015-04-23 21:19:10 +02:00
|
|
|
onmatch: function () {
|
2015-04-21 23:18:59 +02:00
|
|
|
this.applySelect2();
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})(jQuery);
|