2 $.fn.select2.defaults.set("allowClear", true);
3 $.fn.select2.defaults.set("placeholder", "");
4 $.fn.select2.defaults.set("width", "element" );
6 // Internationalization
7 $.fn.select2.defaults.set("language", {
8 errorLoading:function(){ return __("The results could not be loaded"); },
9 inputTooLong:function(args){
10 var n = args.input.length - args.maximum;
11 return __("Please delete %s character(s)").format(n);
13 inputTooShort:function(args){
14 var n = args.minimum - args.input.length;
15 return __("Please enter %s or more characters").format(n);
17 formatResult: function(item) {
18 return $('<div>', {title: item.element[0].title}).text(item.text);
20 loadingMore:function(){ return __("Loading more results…"); },
21 maximumSelected:function(args){
22 return __("You can only select %s item(s)").format(args.maximum);
24 noResults:function(){return __("No results found"); },
25 searching:function(){return __("Searching…"); },
26 removeAllItems:function(){return __("Remove all items"); },
27 removeItem:function(){return __("Remove item"); }
30 $(document).ready(function(){
31 $(".select2").select2();
32 $(".select2").on("select2:clear", function () {
33 $(this).on("select2:opening.cancelOpen", function (evt) {
36 $(this).off("select2:opening.cancelOpen");
41 function kohaSelect2Transport(params, success, failure) {
42 var read_headers = function (data, textStatus, jqXHR) {
44 var link = jqXHR.getResponseHeader('Link') || '';
45 if (link.search(/<([^>]+)>;\s*rel\s*=\s*['"]?next['"]?\s*(,|$)/i) > -1) {
56 var $request = $.ajax(params);
57 $request.then(read_headers).then(success);
58 $request.fail(failure);
64 * Create a new Select2 instance that uses the Koha RESTful API response headers to
65 * read pagination information
66 * @param {Object} config Please see the Select2 documentation for further details
67 * @return {Object} The Select2 instance
70 $.fn.kohaSelect = function(config) {
71 if (config.hasOwnProperty('ajax')) {
72 config.ajax.transport = function(params, success, failure) {
73 var read_headers = function(data, textStatus, jqXHR) {
75 var link = jqXHR.getResponseHeader('Link') || '';
76 if (link.search(/<([^>]+)>;\s*rel\s*=\s*['"]?next['"]?\s*(,|$)/i) > -1) {
87 var $request = $.ajax(params);
88 $request.then(read_headers).then(success);
89 $request.fail(failure);
94 $(this).select2(config);