PageSourceSearch

https://cdn.techniques-ingenieur.fr/lucy/ti/cdn.prod.v5.3.0/assets/plugins/bootstrap-tabcollapse.js

js techniques-ingenieur.fr collected 2026-09-24 09:17:06 UTC 8,736 bytes, 237 lines download raw bytes

1!function ($) {
2
3    "use strict";
4
5    // TABCOLLAPSE CLASS DEFINITION
6    // ======================
7
8    var TabCollapse = function (el, options) {
9        this.options   = options;
10        this.$tabs  = $(el);
11
12        this._accordionVisible = false; //content is attached to tabs at first
13        this._initAccordion();
14        this._checkStateOnResize();
15
16
17        // checkState() has gone to setTimeout for making it possible to attach listeners to
18        // shown-accordion.bs.tabcollapse event on page load.
19        // See https://github.com/flatlogic/bootstrap-tabcollapse/issues/23
20        var that = this;
21        setTimeout(function() {
22          that.checkState();
23        }, 0);
24    };
25
26    TabCollapse.DEFAULTS = {
27        accordionClass: 'visible-xs',
28        tabsClass: 'hidden-xs',
29        accordionTemplate: function(heading, groupId, parentId, active) {
30            return  '<div class="panel panel-default">' +
31                    '   <div class="panel-heading">' +
32                    '      <h4 class="panel-title">' +
33                    '      </h4>' +
34                    '   </div>' +
35                    '   <div id="' + groupId + '" class="panel-collapse collapse ' + (active ? 'in' : '') + '">' +
36                    '       <div class="panel-body js-tabcollapse-panel-body">' +
37                    '       </div>' +
38                    '   </div>' +
39                    '</div>'
40
41        }
42    };
43
44    TabCollapse.prototype.checkState = function(){
45        if (this.$tabs.is(':visible') && this._accordionVisible){
46            this.showTabs();
47            this._accordionVisible = false;
48        } else if (this.$accordion.is(':visible') && !this._accordionVisible){
49            this.showAccordion();
50            this._accordionVisible = true;
51        }
52    };
53
54    TabCollapse.prototype.showTabs = function(){
55        var view = this;
56        this.$tabs.trigger($.Event('show-tabs.bs.tabcollapse'));
57
58        var $panelHeadings = this.$accordion.find('.js-tabcollapse-panel-heading').detach();
59
60        $panelHeadings.each(function() {
61            var $panelHeading = $(this),
62            $parentLi = $panelHeading.data('bs.tabcollapse.parentLi');
63
64            var $oldHeading = view._panelHeadingToTabHeading($panelHeading);
65
66            $parentLi.removeClass('active');
67            if ($parentLi.parent().hasClass('dropdown-menu') && !$parentLi.siblings('li').hasClass('active')) {
68                $parentLi.parent().parent().removeClass('active');
69            }
70
71            if (!$oldHeading.hasClass('collapsed')) {
72                $parentLi.addClass('active');
73                if ($parentLi.parent().hasClass('dropdown-menu')) {
74                    $parentLi.parent().parent().addClass('active');
75                }
76            } else {
77                $oldHeading.removeClass('collapsed');
78            }
79
80            $parentLi.append($panelHeading);
81        });
82
83        if (!$('li').hasClass('active')) {
84            $('li').first().addClass('active')
85        }
86
87        var $panelBodies = this.$accordion.find('.js-tabcollapse-panel-body');
88        $panelBodies.each(function(){
89            var $panelBody = $(this),
90                $tabPane = $panelBody.data('bs.tabcollapse.tabpane');
91            $tabPane.append($panelBody.contents().detach());
92        });
93        this.$accordion.html('');
94
95        if(this.options.updateLinks) {
96            var $tabContents = this.getTabContentElement();
97            $tabContents.find('[data-toggle-was="tab"], [data-toggle-was="pill"]').each(function() {
98                var $el = $(this);
99                var href = $el.attr('href').replace(/-collapse$/g, '');
100                $el.attr({
101                    'data-toggle': $el.attr('data-toggle-was'),
102                    'data-toggle-was': '',
103                    'data-parent': '',
104                    href: href
105                });
106            });
107        }
108
109        this.$tabs.trigger($.Event('shown-tabs.bs.tabcollapse'));
110    };
111
112    TabCollapse.prototype.getTabContentElement = function(){
113        var $tabContents = $(this.options.tabContentSelector);
114        if($tabContents.length === 0) {
115            $tabContents = this.$tabs.siblings('.tab-content');
116        }
117        return $tabContents;
118    };
119
120    TabCollapse.prototype.showAccordion = function(){
121        this.$tabs.trigger($.Event('show-accordion.bs.tabcollapse'));
122
123        var $headings = this.$tabs.find('li:not(.dropdown) [data-toggle="tab"], li:not(.dropdown) [data-toggle="pill"]'),
124            view = this;
125        $headings.each(function(){
126            var $heading = $(this),
127                $parentLi = $heading.parent();
128            $heading.data('bs.tabcollapse.parentLi', $parentLi);
129            view.$accordion.append(view._createAccordionGroup(view.$accordion.attr('id'), $heading.detach()));
130        });
131
132        if(this.options.updateLinks) {
133            var parentId = this.$accordion.attr('id');
134            var $selector = this.$accordion.find('.js-tabcollapse-panel-body');
135            $selector.find('[data-toggle="tab"], [data-toggle="pill"]').each(function() {
136                var $el = $(this);
137                var href = $el.attr('href') + '-collapse';
138                $el.attr({
139                    'data-toggle-was': $el.attr('data-toggle'),
140                    'data-toggle': 'collapse',
141                    'data-parent': '#' + parentId,
142                    href: href
143                });
144            });
145        }
146
147        this.$tabs.trigger($.Event('shown-accordion.bs.tabcollapse'));
148    };
149
150    TabCollapse.prototype._panelHeadingToTabHeading = function($heading) {
151        var href = $heading.attr('href').replace(/-collapse$/g, '');
152        $heading.attr({
153            'data-toggle': 'tab',
154            'href': href,
155            'data-parent': ''
156        });
157        return $heading;
158    };
159
160    TabCollapse.prototype._tabHeadingToPanelHeading = function($heading, groupId, parentId, active) {
161        $heading.addClass('js-tabcollapse-panel-heading ' + (active ? '' : 'collapsed'));
162        $heading.attr({
163            'data-toggle': 'collapse',
164            'data-parent': '#' + parentId,
165            'href': '#' + groupId
166        });
167        return $heading;
168    };
169
170    TabCollapse.prototype._checkStateOnResize = function(){
171        var view = this;
172        $(window).resize(function(){
173            clearTimeout(view._resizeTimeout);
174            view._resizeTimeout = setTimeout(function(){
175                view.checkState();
176            }, 100);
177        });
178    };
179
180
181    TabCollapse.prototype._initAccordion = function(){
182        var randomString = function() {
183            var result = "",
184                possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
185            for( var i=0; i < 5; i++ ) {
186                result += possible.charAt(Math.floor(Math.random() * possible.length));
187            }
188            return result;
189        };
190
191        var srcId = this.$tabs.attr('id'),
192            accordionId = (srcId ? srcId : randomString()) + '-accordion';
193
194        this.$accordion = $('<div class="panel-group ' + this.options.accordionClass + '" id="' + accordionId +'"></div>');
195        this.$tabs.after(this.$accordion);
196        this.$tabs.addClass(this.options.tabsClass);
197        this.getTabContentElement().addClass(this.options.tabsClass);
198    };
199
200    TabCollapse.prototype._createAccordionGroup = function(parentId, $heading){
201        var tabSelector = $heading.attr('data-target'),
202            active = $heading.data('bs.tabcollapse.parentLi').is('.active');
203
204        if (!tabSelector) {
205            tabSelector = $heading.attr('href');
206            tabSelector = tabSelector && tabSelector.replace(/.*(?=#[^\s]*$)/, ''); //strip for ie7
207        }
208
209        var $tabPane = $(tabSelector),
210            groupId = $tabPane.attr('id') + '-collapse',
211            $panel = $(this.options.accordionTemplate($heading, groupId, parentId, active));
212        $panel.find('.panel-heading > .panel-title').append(this._tabHeadingToPanelHeading($heading, groupId, parentId, active));
213        $panel.find('.panel-body').append($tabPane.contents().detach())
214            .data('bs.tabcollapse.tabpane', $tabPane);
215
216        return $panel;
217    };
218
219
220
221    // TABCOLLAPSE PLUGIN DEFINITION
222    // =======================
223
224    $.fn.tabCollapse = function (option) {
225        return this.each(function () {
226            var $this   = $(this);
227            var data    = $this.data('bs.tabcollapse');
228            var options = $.extend({}, TabCollapse.DEFAULTS, $this.data(), typeof option === 'object' && option);
229
230            if (!data) $this.data('bs.tabcollapse', new TabCollapse(this, options));
231        });
232    };
233
234    $.fn.tabCollapse.Constructor = TabCollapse;
235
236
237}(window.jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.