Bug 31646: Add Select2 focus to text search field
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / select2.js
1 /* global __ */
2 $.fn.select2.defaults.set("allowClear", true);
3 $.fn.select2.defaults.set("placeholder", "");
4 $.fn.select2.defaults.set("width", "element" );
5
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);
12     },
13     inputTooShort:function(args){
14         var n = args.minimum - args.input.length;
15         return __("Please enter %s or more characters").format(n);
16     },
17     formatResult: function(item) {
18         return $('<div>', {title: item.element[0].title}).text(item.text);
19     },
20     loadingMore:function(){ return __("Loading more results…"); },
21     maximumSelected:function(args){
22         return __("You can only select %s item(s)").format(args.maximum);
23     },
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"); }
28 });
29
30 $(document).ready(function(){
31     $(".select2").select2();
32     $(".select2").on("select2:clear", function () {
33         $(this).on("select2:opening.cancelOpen", function (evt) {
34             evt.preventDefault();
35
36             $(this).off("select2:opening.cancelOpen");
37         });
38     });
39
40     $(document).on("select2:open", function () {
41         document.querySelector(".select2-container--open .select2-search__field").focus();
42     });
43 });
44
45 (function($) {
46
47     /**
48     * Create a new Select2 instance that uses the Koha RESTful API response headers to
49     * read pagination information
50     * @param  {Object}  config  Please see the Select2 documentation for further details
51     * @return {Object}          The Select2 instance
52     */
53
54     $.fn.kohaSelect = function(config) {
55         if (config.hasOwnProperty('ajax')) {
56             config.ajax.transport = function(params, success, failure) {
57                 var read_headers = function(data, textStatus, jqXHR) {
58                     var more = false;
59                     var link = jqXHR.getResponseHeader('Link') || '';
60                     if (link.search(/<([^>]+)>;\s*rel\s*=\s*['"]?next['"]?\s*(,|$)/i) > -1) {
61                         more = true;
62                     }
63
64                     return {
65                         results: data,
66                         pagination: {
67                             more: more
68                         }
69                     };
70                 };
71                 var $request = $.ajax(params);
72                 $request.then(read_headers).then(success);
73                 $request.fail(failure);
74
75             };
76         }
77
78         $(this).select2(config);
79     };
80 })(jQuery);