integ/lib/bootstrap-multiselect.js
author cavaliet
Tue, 04 Mar 2014 14:48:25 +0100
changeset 7 4681446b722d
parent 1 ad760c317293
permissions -rwxr-xr-x
management command first step
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     1
/**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     2
 * bootstrap-multiselect.js
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     3
 * https://github.com/davidstutz/bootstrap-multiselect
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     4
 *
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     5
 * Copyright 2012, 2013 David Stutz
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     6
 *
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     7
 * Dual licensed under the BSD-3-Clause and the Apache License, Version 2.0.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     8
 */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
     9
!function($) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    10
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    11
    "use strict";// jshint ;_;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    12
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    13
    if (typeof ko !== 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    14
        ko.bindingHandlers.multiselect = {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    15
            init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {},
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    16
            update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    17
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    18
               var config = ko.utils.unwrapObservable(valueAccessor());
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    19
               var selectOptions = allBindingsAccessor().options;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    20
               var ms = $(element).data('multiselect');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    21
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    22
               if (!ms) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    23
                  $(element).multiselect(config);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    24
               }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    25
               else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    26
                  ms.updateOriginalOptions();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    27
                  if (selectOptions && selectOptions().length !== ms.originalOptions.length) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    28
                     $(element).multiselect('rebuild');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    29
                  }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    30
               }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    31
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    32
        };
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    33
    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    34
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    35
    /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    36
     * Constructor to create a new multiselect using the given select.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    37
     * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    38
     * @param {jQuery} select
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    39
     * @param {Object} options
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    40
     * @returns {Multiselect}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    41
     */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    42
    function Multiselect(select, options) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    43
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    44
        this.options = this.mergeOptions(options);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    45
        this.$select = $(select);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    46
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    47
        // Initialization.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    48
        // We have to clone to create a new reference.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    49
        this.originalOptions = this.$select.clone()[0].options;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    50
        this.query = '';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    51
        this.searchTimeout = null;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    52
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    53
        this.options.multiple = this.$select.attr('multiple') === "multiple";
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    54
        this.options.onChange = $.proxy(this.options.onChange, this);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    55
        this.options.onDropdownShow = $.proxy(this.options.onDropdownShow, this);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    56
        this.options.onDropdownHide = $.proxy(this.options.onDropdownHide, this);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    57
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    58
        // Build select all if enabled.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    59
        this.buildContainer();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    60
        this.buildButton();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    61
        this.buildSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    62
        this.buildDropdown();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    63
        this.buildDropdownOptions();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    64
        this.buildFilter();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    65
        
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    66
        this.updateButtonText();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    67
        this.updateSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    68
        
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    69
        this.$select.hide().after(this.$container);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    70
    };
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    71
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    72
    Multiselect.prototype = {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    73
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    74
        defaults: {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    75
            /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    76
             * Default text function will either print 'None selected' in case no
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    77
             * option is selected or a list of the selected options up to a length of 3 selected options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    78
             * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    79
             * @param {jQuery} options
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    80
             * @param {jQuery} select
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    81
             * @returns {String}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    82
             */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    83
            buttonText: function(options, select) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    84
                if (options.length === 0) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    85
                    return this.nonSelectedText + ' <b class="caret"></b>';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    86
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    87
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    88
                    if (options.length > this.numberDisplayed) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    89
                        return options.length + ' ' + this.nSelectedText + ' <b class="caret"></b>';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    90
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    91
                    else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    92
                        var selected = '';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    93
                        options.each(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    94
                            var label = ($(this).attr('label') !== undefined) ? $(this).attr('label') : $(this).html();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    95
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    96
                            selected += label + ', ';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    97
                        });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    98
                        return selected.substr(0, selected.length - 2) + ' <b class="caret"></b>';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
    99
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   100
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   101
            },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   102
            /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   103
             * Updates the title of the button similar to the buttonText function.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   104
             * @param {jQuery} options
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   105
             * @param {jQuery} select
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   106
             * @returns {@exp;selected@call;substr}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   107
             */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   108
            buttonTitle: function(options, select) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   109
                if (options.length === 0) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   110
                    return this.nonSelectedText;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   111
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   112
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   113
                    var selected = '';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   114
                    options.each(function () {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   115
                        selected += $(this).text() + ', ';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   116
                    });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   117
                    return selected.substr(0, selected.length - 2);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   118
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   119
            },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   120
            /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   121
             * Create a label.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   122
             * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   123
             * @param {jQuery} element
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   124
             * @returns {String}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   125
             */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   126
            label: function(element){
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   127
                return $(element).attr('label') || $(element).html();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   128
            },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   129
            /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   130
             * Triggered on change of the multiselect.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   131
             * Not triggered when selecting/deselecting options manually.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   132
             * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   133
             * @param {jQuery} option
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   134
             * @param {Boolean} checked
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   135
             */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   136
            onChange : function(option, checked) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   137
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   138
            },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   139
            /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   140
             * Triggered when the dropdown is shown.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   141
             * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   142
             * @param {jQuery} event
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   143
             */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   144
            onDropdownShow: function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   145
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   146
            },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   147
            /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   148
             * Triggered when the dropdown is hidden.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   149
             * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   150
             * @param {jQuery} event
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   151
             */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   152
            onDropdownHide: function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   153
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   154
            },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   155
            buttonClass: 'btn btn-default',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   156
            dropRight: false,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   157
            selectedClass: 'active',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   158
            buttonWidth: 'auto',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   159
            buttonContainer: '<div class="btn-group" />',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   160
            // Maximum height of the dropdown menu.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   161
            // If maximum height is exceeded a scrollbar will be displayed.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   162
            maxHeight: false,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   163
            includeSelectAllOption: false,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   164
            selectAllText: ' Select all',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   165
            selectAllValue: 'multiselect-all',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   166
            enableFiltering: false,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   167
            enableCaseInsensitiveFiltering: false,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   168
            filterPlaceholder: 'Search',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   169
            // possible options: 'text', 'value', 'both'
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   170
            filterBehavior: 'text',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   171
            preventInputChangeEvent: false,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   172
            nonSelectedText: 'None selected',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   173
            nSelectedText: 'selected',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   174
            numberDisplayed: 3
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   175
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   176
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   177
        templates: {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   178
            button: '<button type="button" class="multiselect dropdown-toggle" data-toggle="dropdown"></button>',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   179
            ul: '<ul class="multiselect-container dropdown-menu"></ul>',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   180
            filter: '<div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span><input class="form-control multiselect-search" type="text"></div>',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   181
            li: '<li><a href="javascript:void(0);"><label></label></a></li>',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   182
            divider: '<li class="divider"></li>',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   183
            liGroup: '<li><label class="multiselect-group"></label></li>'
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   184
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   185
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   186
        constructor: Multiselect,
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   187
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   188
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   189
         * Builds the container of the multiselect.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   190
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   191
        buildContainer: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   192
            this.$container = $(this.options.buttonContainer);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   193
            this.$container.on('show.bs.dropdown', this.options.onDropdownShow);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   194
            this.$container.on('hide.bs.dropdown', this.options.onDropdownHide);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   195
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   196
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   197
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   198
         * Builds the button of the multiselect.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   199
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   200
        buildButton: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   201
            this.$button = $(this.templates.button).addClass(this.options.buttonClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   202
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   203
            // Adopt active state.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   204
            if (this.$select.prop('disabled')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   205
                this.disable();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   206
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   207
            else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   208
                this.enable();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   209
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   210
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   211
            // Manually add button width if set.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   212
            if (this.options.buttonWidth && this.options.buttonWidth != 'auto') {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   213
                this.$button.css({
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   214
                    'width' : this.options.buttonWidth
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   215
                });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   216
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   217
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   218
            // Keep the tab index from the select.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   219
            var tabindex = this.$select.attr('tabindex');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   220
            if (tabindex) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   221
                this.$button.attr('tabindex', tabindex);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   222
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   223
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   224
            this.$container.prepend(this.$button);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   225
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   226
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   227
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   228
         * Builds the ul representing the dropdown menu.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   229
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   230
        buildDropdown: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   231
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   232
            // Build ul.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   233
            this.$ul = $(this.templates.ul);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   234
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   235
            if (this.options.dropRight) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   236
                this.$ul.addClass('pull-right');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   237
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   238
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   239
            // Set max height of dropdown menu to activate auto scrollbar.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   240
            if (this.options.maxHeight) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   241
                // TODO: Add a class for this option to move the css declarations.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   242
                this.$ul.css({
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   243
                    'max-height': this.options.maxHeight + 'px',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   244
                    'overflow-y': 'auto',
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   245
                    'overflow-x': 'hidden'
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   246
                });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   247
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   248
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   249
            this.$container.append(this.$ul);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   250
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   251
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   252
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   253
         * Build the dropdown options and binds all nessecary events.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   254
         * Uses createDivider and createOptionValue to create the necessary options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   255
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   256
        buildDropdownOptions: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   257
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   258
            this.$select.children().each($.proxy(function(index, element) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   259
                
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   260
                // Support optgroups and options without a group simultaneously.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   261
                var tag = $(element).prop('tagName')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   262
                    .toLowerCase();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   263
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   264
                if (tag === 'optgroup') {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   265
                    this.createOptgroup(element);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   266
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   267
                else if (tag === 'option') {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   268
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   269
                    if ($(element).data('role') === 'divider') {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   270
                        this.createDivider();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   271
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   272
                    else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   273
                        this.createOptionValue(element);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   274
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   275
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   276
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   277
                
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   278
                // Other illegal tags will be ignored.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   279
            }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   280
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   281
            // Bind the change event on the dropdown elements.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   282
            $('li input', this.$ul).on('change', $.proxy(function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   283
                var checked = $(event.target).prop('checked') || false;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   284
                var isSelectAllOption = $(event.target).val() === this.options.selectAllValue;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   285
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   286
                // Apply or unapply the configured selected class.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   287
                if (this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   288
                    if (checked) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   289
                        $(event.target).parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   290
                            .addClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   291
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   292
                    else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   293
                        $(event.target).parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   294
                            .removeClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   295
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   296
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   297
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   298
                // Get the corresponding option.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   299
                var value = $(event.target).val();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   300
                var $option = this.getOptionByValue(value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   301
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   302
                var $optionsNotThis = $('option', this.$select).not($option);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   303
                var $checkboxesNotThis = $('input', this.$container).not($(event.target));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   304
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   305
                if (isSelectAllOption) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   306
                    if (this.$select[0][0].value === this.options.selectAllValue) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   307
                        var values = [];
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   308
                        var options = $('option[value!="' + this.options.selectAllValue + '"]', this.$select);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   309
                        for (var i = 0; i < options.length; i++) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   310
                            // Additionally check whether the option is visible within the dropcown.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   311
                            if (options[i].value !== this.options.selectAllValue && this.getInputByValue(options[i].value).is(':visible')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   312
                                values.push(options[i].value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   313
                            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   314
                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   315
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   316
                        if (checked) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   317
                            this.select(values);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   318
                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   319
                        else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   320
                            this.deselect(values);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   321
                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   322
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   323
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   324
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   325
                if (checked) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   326
                    $option.prop('selected', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   327
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   328
                    if (this.options.multiple) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   329
                        // Simply select additional option.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   330
                        $option.prop('selected', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   331
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   332
                    else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   333
                        // Unselect all other options and corresponding checkboxes.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   334
                        if (this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   335
                            $($checkboxesNotThis).parents('li').removeClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   336
                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   337
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   338
                        $($checkboxesNotThis).prop('checked', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   339
                        $optionsNotThis.prop('selected', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   340
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   341
                        // It's a single selection, so close.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   342
                        this.$button.click();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   343
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   344
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   345
                    if (this.options.selectedClass === "active") {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   346
                        $optionsNotThis.parents("a").css("outline", "");
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   347
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   348
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   349
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   350
                    // Unselect option.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   351
                    $option.prop('selected', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   352
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   353
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   354
                this.$select.change();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   355
                this.options.onChange($option, checked);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   356
                
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   357
                this.updateButtonText();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   358
                this.updateSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   359
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   360
                if(this.options.preventInputChangeEvent) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   361
                    return false;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   362
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   363
            }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   364
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   365
            $('li a', this.$ul).on('touchstart click', function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   366
                event.stopPropagation();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   367
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   368
                if (event.shiftKey) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   369
                    var checked = $(event.target).prop('checked') || false;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   370
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   371
                    if (checked) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   372
                        var prev = $(event.target).parents('li:last')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   373
                            .siblings('li[class="active"]:first');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   374
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   375
                        var currentIdx = $(event.target).parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   376
                            .index();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   377
                        var prevIdx = prev.index();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   378
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   379
                        if (currentIdx > prevIdx) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   380
                            $(event.target).parents("li:last").prevUntil(prev).each(
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   381
                                function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   382
                                    $(this).find("input:first").prop("checked", true)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   383
                                        .trigger("change");
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   384
                                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   385
                            );
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   386
                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   387
                        else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   388
                            $(event.target).parents("li:last").nextUntil(prev).each(
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   389
                                function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   390
                                    $(this).find("input:first").prop("checked", true)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   391
                                        .trigger("change");
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   392
                                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   393
                            );
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   394
                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   395
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   396
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   397
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   398
                $(event.target).blur();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   399
            });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   400
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   401
            // Keyboard support.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   402
            this.$container.on('keydown', $.proxy(function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   403
                if ($('input[type="text"]', this.$container).is(':focus')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   404
                    return;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   405
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   406
                if ((event.keyCode === 9 || event.keyCode === 27)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   407
                        && this.$container.hasClass('open')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   408
                    
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   409
                    // Close on tab or escape.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   410
                    this.$button.click();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   411
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   412
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   413
                    var $items = $(this.$container).find("li:not(.divider):visible a");
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   414
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   415
                    if (!$items.length) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   416
                        return;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   417
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   418
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   419
                    var index = $items.index($items.filter(':focus'));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   420
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   421
                    // Navigation up.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   422
                    if (event.keyCode === 38 && index > 0) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   423
                        index--;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   424
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   425
                    // Navigate down.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   426
                    else if (event.keyCode === 40 && index < $items.length - 1) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   427
                        index++;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   428
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   429
                    else if (!~index) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   430
                        index = 0;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   431
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   432
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   433
                    var $current = $items.eq(index);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   434
                    $current.focus();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   435
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   436
                    if (event.keyCode === 32 || event.keyCode === 13) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   437
                        var $checkbox = $current.find('input');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   438
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   439
                        $checkbox.prop("checked", !$checkbox.prop("checked"));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   440
                        $checkbox.change();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   441
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   442
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   443
                    event.stopPropagation();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   444
                    event.preventDefault();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   445
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   446
            }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   447
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   448
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   449
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   450
         * Create an option using the given select option.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   451
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   452
         * @param {jQuery} element
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   453
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   454
        createOptionValue: function(element) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   455
            if ($(element).is(':selected')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   456
                $(element).prop('selected', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   457
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   458
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   459
            // Support the label attribute on options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   460
            var label = this.options.label(element);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   461
            var value = $(element).val();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   462
            var inputType = this.options.multiple ? "checkbox" : "radio";
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   463
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   464
            var $li = $(this.templates.li);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   465
            $('label', $li).addClass(inputType);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   466
            $('label', $li).append('<input type="' + inputType + '" />');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   467
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   468
            var selected = $(element).prop('selected') || false;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   469
            var $checkbox = $('input', $li);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   470
            $checkbox.val(value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   471
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   472
            if (value === this.options.selectAllValue) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   473
                $checkbox.parent().parent()
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   474
                    .addClass('multiselect-all');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   475
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   476
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   477
            $('label', $li).append(" " + label);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   478
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   479
            this.$ul.append($li);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   480
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   481
            if ($(element).is(':disabled')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   482
                $checkbox.attr('disabled', 'disabled')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   483
                    .prop('disabled', true)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   484
                    .parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   485
                    .addClass('disabled');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   486
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   487
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   488
            $checkbox.prop('checked', selected);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   489
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   490
            if (selected && this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   491
                $checkbox.parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   492
                    .addClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   493
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   494
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   495
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   496
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   497
         * Creates a divider using the given select option.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   498
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   499
         * @param {jQuery} element
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   500
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   501
        createDivider: function(element) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   502
            var $divider = $(this.templates.divider);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   503
            this.$ul.append($divider);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   504
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   505
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   506
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   507
         * Creates an optgroup.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   508
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   509
         * @param {jQuery} group
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   510
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   511
        createOptgroup: function(group) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   512
            var groupName = $(group).prop('label');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   513
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   514
            // Add a header for the group.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   515
            var $li = $(this.templates.liGroup);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   516
            $('label', $li).text(groupName);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   517
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   518
            this.$ul.append($li);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   519
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   520
            // Add the options of the group.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   521
            $('option', group).each($.proxy(function(index, element) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   522
                this.createOptionValue(element);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   523
            }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   524
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   525
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   526
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   527
         * Build the selct all.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   528
         * Checks if a select all ahs already been created.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   529
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   530
        buildSelectAll: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   531
            var alreadyHasSelectAll = this.hasSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   532
            
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   533
            // If options.includeSelectAllOption === true, add the include all checkbox.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   534
            if (this.options.includeSelectAllOption && this.options.multiple && !alreadyHasSelectAll) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   535
                this.$select.prepend('<option value="' + this.options.selectAllValue + '">' + this.options.selectAllText + '</option>');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   536
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   537
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   538
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   539
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   540
         * Builds the filter.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   541
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   542
        buildFilter: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   543
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   544
            // Build filter if filtering OR case insensitive filtering is enabled and the number of options exceeds (or equals) enableFilterLength.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   545
            if (this.options.enableFiltering || this.options.enableCaseInsensitiveFiltering) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   546
                var enableFilterLength = Math.max(this.options.enableFiltering, this.options.enableCaseInsensitiveFiltering);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   547
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   548
                if (this.$select.find('option').length >= enableFilterLength) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   549
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   550
                    this.$filter = $(this.templates.filter);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   551
                    $('input', this.$filter).attr('placeholder', this.options.filterPlaceholder);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   552
                    this.$ul.prepend(this.$filter);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   553
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   554
                    this.$filter.val(this.query).on('click', function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   555
                        event.stopPropagation();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   556
                    }).on('input keydown', $.proxy(function(event) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   557
                        // This is useful to catch "keydown" events after the browser has updated the control.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   558
                        clearTimeout(this.searchTimeout);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   559
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   560
                        this.searchTimeout = this.asyncFunction($.proxy(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   561
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   562
                            if (this.query !== event.target.value) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   563
                                this.query = event.target.value;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   564
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   565
                                $.each($('li', this.$ul), $.proxy(function(index, element) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   566
                                    var value = $('input', element).val();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   567
                                    var text = $('label', element).text();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   568
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   569
                                    if (value !== this.options.selectAllValue && text) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   570
                                        // by default lets assume that element is not
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   571
                                        // interesting for this search
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   572
                                        var showElement = false;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   573
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   574
                                        var filterCandidate = '';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   575
                                        if ((this.options.filterBehavior === 'text' || this.options.filterBehavior === 'both')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   576
                                            filterCandidate = text;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   577
                                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   578
                                        if ((this.options.filterBehavior === 'value' || this.options.filterBehavior === 'both')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   579
                                            filterCandidate = value;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   580
                                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   581
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   582
                                        if (this.options.enableCaseInsensitiveFiltering && filterCandidate.toLowerCase().indexOf(this.query.toLowerCase()) > -1) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   583
                                            showElement = true;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   584
                                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   585
                                        else if (filterCandidate.indexOf(this.query) > -1) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   586
                                            showElement = true;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   587
                                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   588
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   589
                                        if (showElement) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   590
                                            $(element).show();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   591
                                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   592
                                        else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   593
                                            $(element).hide();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   594
                                        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   595
                                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   596
                                }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   597
                            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   598
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   599
                            // TODO: check whether select all option needs to be updated.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   600
                        }, this), 300, this);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   601
                    }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   602
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   603
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   604
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   605
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   606
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   607
         * Unbinds the whole plugin.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   608
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   609
        destroy: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   610
            this.$container.remove();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   611
            this.$select.show();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   612
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   613
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   614
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   615
         * Refreshs the multiselect based on the selected options of the select.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   616
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   617
        refresh: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   618
            $('option', this.$select).each($.proxy(function(index, element) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   619
                var $input = $('li input', this.$ul).filter(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   620
                    return $(this).val() === $(element).val();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   621
                });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   622
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   623
                if ($(element).is(':selected')) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   624
                    $input.prop('checked', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   625
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   626
                    if (this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   627
                        $input.parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   628
                            .addClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   629
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   630
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   631
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   632
                    $input.prop('checked', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   633
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   634
                    if (this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   635
                        $input.parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   636
                            .removeClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   637
                    }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   638
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   639
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   640
                if ($(element).is(":disabled")) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   641
                    $input.attr('disabled', 'disabled')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   642
                        .prop('disabled', true)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   643
                        .parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   644
                        .addClass('disabled');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   645
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   646
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   647
                    $input.prop('disabled', false)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   648
                        .parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   649
                        .removeClass('disabled');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   650
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   651
            }, this));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   652
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   653
            this.updateButtonText();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   654
            this.updateSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   655
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   656
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   657
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   658
         * Select all options of the given values.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   659
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   660
         * @param {Array} selectValues
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   661
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   662
        select: function(selectValues) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   663
            if(selectValues && !$.isArray(selectValues)) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   664
                selectValues = [selectValues];
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   665
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   666
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   667
            for (var i = 0; i < selectValues.length; i++) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   668
                var value = selectValues[i];
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   669
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   670
                var $option = this.getOptionByValue(value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   671
                var $checkbox = this.getInputByValue(value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   672
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   673
                if (this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   674
                    $checkbox.parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   675
                        .addClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   676
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   677
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   678
                $checkbox.prop('checked', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   679
                $option.prop('selected', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   680
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   681
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   682
            this.updateButtonText();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   683
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   684
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   685
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   686
         * Deselects all options of the given values.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   687
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   688
         * @param {Array} deselectValues
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   689
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   690
        deselect: function(deselectValues) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   691
            if(deselectValues && !$.isArray(deselectValues)) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   692
                deselectValues = [deselectValues];
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   693
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   694
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   695
            for (var i = 0; i < deselectValues.length; i++) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   696
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   697
                var value = deselectValues[i];
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   698
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   699
                var $option = this.getOptionByValue(value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   700
                var $checkbox = this.getInputByValue(value);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   701
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   702
                if (this.options.selectedClass) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   703
                    $checkbox.parents('li')
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   704
                        .removeClass(this.options.selectedClass);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   705
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   706
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   707
                $checkbox.prop('checked', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   708
                $option.prop('selected', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   709
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   710
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   711
            this.updateButtonText();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   712
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   713
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   714
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   715
         * Rebuild the plugin.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   716
         * Rebuilds the dropdown, the filter and the select all option.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   717
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   718
        rebuild: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   719
            this.$ul.html('');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   720
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   721
            // Remove select all option in select.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   722
            $('option[value="' + this.options.selectAllValue + '"]', this.$select).remove();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   723
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   724
            // Important to distinguish between radios and checkboxes.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   725
            this.options.multiple = this.$select.attr('multiple') === "multiple";
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   726
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   727
            this.buildSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   728
            this.buildDropdownOptions();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   729
            this.buildFilter();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   730
            
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   731
            this.updateButtonText();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   732
            this.updateSelectAll();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   733
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   734
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   735
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   736
         * The provided data will be used to build the dropdown.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   737
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   738
         * @param {Array} dataprovider
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   739
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   740
        dataprovider: function(dataprovider) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   741
            var optionDOM = "";
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   742
            dataprovider.forEach(function (option) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   743
                optionDOM += '<option value="' + option.value + '">' + option.label + '</option>';
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   744
            });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   745
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   746
            this.$select.html(optionDOM);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   747
            this.rebuild();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   748
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   749
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   750
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   751
         * Enable the multiselect.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   752
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   753
        enable: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   754
            this.$select.prop('disabled', false);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   755
            this.$button.prop('disabled', false)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   756
                .removeClass('disabled');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   757
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   758
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   759
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   760
         * Disable the multiselect.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   761
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   762
        disable: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   763
            this.$select.prop('disabled', true);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   764
            this.$button.prop('disabled', true)
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   765
                .addClass('disabled');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   766
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   767
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   768
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   769
         * Set the options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   770
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   771
         * @param {Array} options
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   772
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   773
        setOptions: function(options) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   774
            this.options = this.mergeOptions(options);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   775
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   776
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   777
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   778
         * Merges the given options with the default options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   779
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   780
         * @param {Array} options
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   781
         * @returns {Array}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   782
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   783
        mergeOptions: function(options) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   784
            return $.extend({}, this.defaults, options);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   785
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   786
        
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   787
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   788
         * Checks whether a select all option is present.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   789
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   790
         * @returns {Boolean}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   791
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   792
        hasSelectAll: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   793
            return this.$select[0][0] ? this.$select[0][0].value === this.options.selectAllValue : false;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   794
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   795
        
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   796
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   797
         * Updates the select all option based on the currently selected options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   798
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   799
        updateSelectAll: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   800
            if (this.hasSelectAll()) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   801
                var selected = this.getSelected();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   802
                
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   803
                if (selected.length === $('option', this.$select).length - 1) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   804
                    this.select(this.options.selectAllValue);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   805
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   806
                else {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   807
                    this.deselect(this.options.selectAllValue);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   808
                }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   809
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   810
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   811
        
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   812
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   813
         * Update the button text and its title base don the currenty selected options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   814
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   815
        updateButtonText: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   816
            var options = this.getSelected();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   817
            
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   818
            // First update the displayed button text.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   819
            $('button', this.$container).html(this.options.buttonText(options, this.$select));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   820
            
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   821
            // Now update the title attribute of the button.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   822
            $('button', this.$container).attr('title', this.options.buttonTitle(options, this.$select));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   823
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   824
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   825
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   826
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   827
         * Get all selected options.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   828
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   829
         * @returns {jQUery}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   830
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   831
        getSelected: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   832
            return $('option[value!="' + this.options.selectAllValue + '"]:selected', this.$select).filter(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   833
                return $(this).prop('selected');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   834
            });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   835
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   836
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   837
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   838
         * Gets a select option by its value.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   839
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   840
         * @param {String} value
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   841
         * @returns {jQuery}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   842
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   843
        getOptionByValue: function(value) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   844
            return $('option', this.$select).filter(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   845
                return $(this).val() === value;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   846
            });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   847
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   848
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   849
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   850
         * Get the input (radio/checkbox) by its value.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   851
         * 
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   852
         * @param {String} value
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   853
         * @returns {jQuery}
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   854
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   855
        getInputByValue: function(value) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   856
            return $('li input', this.$ul).filter(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   857
                return $(this).val() === value;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   858
            });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   859
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   860
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   861
        /**
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   862
         * Used for knockout integration.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   863
         */
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   864
        updateOriginalOptions: function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   865
            this.originalOptions = this.$select.clone()[0].options;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   866
        },
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   867
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   868
        asyncFunction: function(callback, timeout, self) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   869
            var args = Array.prototype.slice.call(arguments, 3);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   870
            return setTimeout(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   871
                callback.apply(self || window, args);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   872
            }, timeout);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   873
        }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   874
    };
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   875
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   876
    $.fn.multiselect = function(option, parameter) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   877
        return this.each(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   878
            var data = $(this).data('multiselect');
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   879
            var options = typeof option === 'object' && option;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   880
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   881
            // Initialize the multiselect.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   882
            if (!data) {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   883
                $(this).data('multiselect', ( data = new Multiselect(this, options)));
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   884
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   885
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   886
            // Call multiselect method.
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   887
            if (typeof option === 'string') {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   888
                data[option](parameter);
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   889
            }
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   890
        });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   891
    };
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   892
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   893
    $.fn.multiselect.Constructor = Multiselect;
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   894
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   895
    $(function() {
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   896
        $("select[data-role=multiselect]").multiselect();
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   897
    });
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   898
ad760c317293 first commit with pdf and more or less integration (for real)
cavaliet
parents:
diff changeset
   899
}(window.jQuery);