forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterms.js
147 lines (134 loc) · 6.7 KB
/
terms.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
"jquery"
], function($){
'use strict';
$.fn.terms = function(args){
// default
var defaults = {
start:0,
wrapper:'',
showAnchor:'',
effects:'slide'
};
var options = $.extend(defaults, args);
this.each(function() {
var obj = $(this),
wrapper = (options.wrapper !== '') ? '> ' + options.wrapper : '',
switches = $(wrapper + '> [data-section="title"] > [data-toggle="switch"]',obj),
terms = $(wrapper + '> [data-section="content"]',obj),
t = switches.length,
marginTop = $(switches[0]).closest('[data-section="title"]').css('position') == 'absolute' ? 0 : null,
title,
current,
init = function() {
if (t > 0) {
if($(switches[0]).closest('[data-section="title"]').css('display')=='table-cell') {
obj.addClass('adjusted');
var linksList;
if (obj[0].tagName=='DL') {
linksList = $('<dd>');
} else {
linksList = $('<div>');
}
linksList.addClass('sections-nav');
obj.prepend(linksList);
for (var i=0; i < t; i++) {
title = $(switches[i]).html();
var classes = $(switches[i]).closest('[data-section="title"]').attr('class');
var dataSection = $(switches[i]).closest('[data-section="title"]').attr('data-section');
var itemHref = $(switches[i]).attr('href');
var itemClass = $(switches[i]).attr('class');
$(switches[i]).parent('[data-section="title"]').hide();
switches[i] = $('<a/>',{
href: itemHref,
'class' : itemClass,
html: title
}).appendTo(linksList);
$(switches[i]).wrap('<strong class="'+classes+'" data-section="'+dataSection+'" />');
}
}
$(switches).each(function(ind,el){
$(el).click(function(event){
event.preventDefault();
showItem(ind);
});
if (marginTop !== null) {
$(el).closest('[data-section="title"]').css({'top' : marginTop + 'px'});
marginTop = marginTop + $(el).closest('[data-section="title"]').outerHeight(true);
obj.css({'min-height' : marginTop + 'px' });
}
});
var fromUrl = false;
if (window.location.hash.length > 0) {
$(terms).each(function(ind,el) {
if ( '#info-'+$(el).attr('id') == window.location.hash) {
showItem(ind);
$('html, body').animate({
scrollTop: $(switches[ind]).offset().top
}, 700);
fromUrl = true;
}
});
}
if (fromUrl === false) {
if ( options.start % 1 === 0 ) {
current = options.start + 1;
showItem(options.start);
} else {
$(terms).each(function(ind,el) {
if ( $(el).attr('id') == options.start) {
current = ind + 1;
showItem(ind);
$('html, body').animate({
scrollTop: $(switches[ind]).offset().top
}, 700);
}
});
}
}
}
},
showItem = function(item) {
if (item != current && !$(switches[item]).closest('[data-section="title"]').hasClass('disabled') ) {
$(switches).closest('[data-section="title"]').removeClass('active');
if (options.wrapper !== '') {
$(switches).parent().parent().removeClass('active');
}
$(terms).removeClass('active');
$(switches[item]).closest('[data-section="title"]').addClass('active');
if (options.wrapper !== '') {
$(switches[current]).parent().parent().addClass('active');
}
$(terms[item]).addClass('active');
/*if ($(terms[item]).attr('id')) {
scr = document.body.scrollTop;
window.location.hash='#tab-' + $(terms[item]).attr('id');
document.body.scrollTop = scr;
}*/
current = item;
} else if (
// Check if this is accordion width as criteria for now
(obj.attr('data-sections') == 'accordion' ||
$(switches[item]).closest('[data-section="title"]').css('width') == obj.css('width')
) &&
item == current && !$(switches[item]).closest('[data-section="title"]').hasClass('disabled')
) {
$(switches).closest('[data-section="title"]').removeClass('active');
if (options.wrapper !== '') {
$(switches).parent().parent().removeClass('active');
}
$(terms).removeClass('active');
current = -1;
}
};
init();
});
};
return function(data, el){
$(el).terms(data);
};
});