-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynatab.js
77 lines (59 loc) · 2.18 KB
/
dynatab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*!
* Dynamic tab management for foundation 5
* http://github.com/general03
*
* @author RIGAUDIE David
* @version 0.1.0
*/
(function (global) {
'use strict';
// Storage variable
var dynatab = {};
// Server parameters
dynatab.contentId = 'content';
dynatab.ajaxType = 'POST';
dynatab.prefixIdTab = '';
dynatab.callback = function(){};
/*
* Public methods
*/
// Create Tab
dynatab.createTab = function (tabClass, contentClass, textContentTab, urlAjax, dataAjax) {
$.ajax(
{
url : urlAjax,
data: dataAjax,
type: dynatab.ajaxType
}).done(function( data ) {
$('.'+contentClass+' section').removeClass('active');
$('.'+contentClass).append(data);
// <input type="hidden" name="id-tab-section" />
var idTable = dynatab.prefixIdTab + '-' + dataAjax["id-tab-section"];
var liContent = $('<li class="tab-title active" role="presentational" >')
.append('<a href="#'+idTable+'" class="left" role="tab" aria-selected="true" aria-controls="'+idTable+'">'+textContentTab+'</a>')
.append('<a class="right tab-action__close" id="tab--'+idTable+'__close">×</a>')
.append('</li>');
// Delete the current active tab
$('.'+tabClass+' .tab-title').removeClass('active');
// Add the tab linked to the table
$('#'+dynatab.contentId+' ul.'+tabClass).append(liContent);
// Add event to delete the tab
$('#tab--'+idTable+'__close').click(function(e){
// Activate the next tab
var ulParent = $(e.target.parentElement).parent();
$('#'+idTable).remove();
$(this).parent().remove();
ulParent.children().last().addClass('active');
// Activate the content linked to the tab
if($('ul.'+tabClass).find('li:last').length > 0)
{
var idTab = $('ul.'+tabClass).find('li:last').find('a:first').get(0).getAttribute('aria-controls');
$('#'+idTab).addClass('active');
}
});
dynatab.callback();
}).fail(function(e){console.log(e)});
};
// Export dynatab into global space
global.dynatab = dynatab;
}(window));