silverstripe-tagfield/js/TagField.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

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 () {
$(this)
2015-04-21 23:18:59 +02:00
.show() // The field needs to be visible so Select2 evaluates the width correctly.
2015-04-14 03:15:28 +02:00
.removeClass('chzn-done')
.removeClass('has-chzn')
.next()
.remove();
return $(this);
};
$.entwine('ss', function($) {
$('.silverstripe-tag-field + .chzn-container').entwine({
2015-04-21 23:18:59 +02:00
applySelect2: function () {
var self = this,
$select = $(this).prev();
// 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);
}
$select
.chosenDestroy()
.select2({
'tags': true,
'tokenSeparators': [',', ' ']
});
2015-04-14 03:15:28 +02:00
/*
* Delay a cycle so select2 is initialised before
* selecting values (if data-selected-values is present).
2015-04-14 03:15:28 +02:00
*/
setTimeout(function () {
if ($select.attr('data-selected-values')) {
var values = $select.attr('data-selected-values');
2015-04-14 03:15:28 +02:00
$select.select2('val', values.split(','));
}
2015-04-14 03:15:28 +02:00
}, 0);
2015-04-21 23:18:59 +02:00
},
onmatch: function() {
this.applySelect2();
2015-04-14 03:15:28 +02:00
}
});
});
})(jQuery);