-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathjquery.metro.js
More file actions
180 lines (156 loc) · 5.1 KB
/
jquery.metro.js
File metadata and controls
180 lines (156 loc) · 5.1 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* jqMetro
* JQUERY PLUGIN FOR METRO UI CONTROLS
*
* Copyright (c) 2011 Mohammad Valipour (http://manorey.net/mohblog)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
*/
(function($) {
let defaults = {
animationDuration: 150,
headerOpacity: 0.5,
fixedHeaders: false,
headerSelector: function(item) {
return item.children('h3').first();
},
itemSelector: function(item) {
return item.children('.pivot-item');
},
headerItemTemplate: function() {
return $("<span class='header' />");
},
pivotItemTemplate: function() {
return $("<div class='pivotItem' />");
},
itemsTemplate: function() {
return $("<div class='items' />");
},
headersTemplate: function() {
return $("<div class='headers' />");
},
controlInitialized: undefined,
selectedItemChanged: undefined
};
$.fn.metroPivot = function(settings) {
if (this.length > 1) {
return this.each(function(index, el) {
$(el).wp7Pivot(settings);
});
}
$.extend(this, defaults, settings);
$.extend(this, {
animating: false,
headers: undefined,
items: undefined,
goToNext: function() {
if (this.animating) return;
this.headers.children('.current').next().trigger('click');
},
goToPrevious: function() {
if (this.animating) return;
this.headers.children('.header').last().trigger('click');
},
goToItemByName: function(header) {
if (this.animating) return;
this.headers.children(':contains(' + header + ')').first().trigger('click');
},
goToItemByIndex: function(index) {
if (this.animating) return;
this.headers.children().eq(index).trigger('click');
},
initialize: function() {
let pivot = this;
// define sections
let headers = pivot.headersTemplate();
let items = pivot.itemsTemplate();
pivot.itemSelector(pivot).each(function(index, el) {
el = $(el);
let h3Element = pivot.headerSelector(el);
if (h3Element.length == 0) return;
let headerItem = pivot.headerItemTemplate().html(h3Element.html()).fadeTo(0, pivot.headerOpacity);
let pivotItem = pivot.pivotItemTemplate().append(el).hide();
if (index == 0) {
headerItem.addClass('current').fadeTo(0, 1);
pivotItem.addClass('current').show();
}
headerItem.attr('index', index);
headerItem.click(function() {
pivot.pivotHeader_Click($(this));
});
headers.append(headerItem);
items.append(pivotItem);
h3Element.remove();
});
pivot.append(headers).append(items);
pivot.headers = headers;
pivot.items = items;
this.data('controller', pivot);
if (this.controlInitialized != undefined) {
this.controlInitialized();
}
},
setCurrentHeader: function(header) {
let pivot = this;
// make current header a normal one
this.headers.children('.header.current').removeClass('current').fadeTo(0, this.headerOpacity);
// make selected header to current
header.addClass('current').fadeTo(0, 1);
if (pivot.fixedHeaders == false) {
// create a copy for fadeout navigation
let copy = header.prevAll().clone();
// detach items to move to end of headers
let detached = $(header.prevAll().detach().toArray().reverse());
// copy animation items to beginning and animate them
$('<span />')
.append(copy)
.prependTo(pivot.headers)
.animate({ width: 0, opacity: 0 }, pivot.animationDuration, function() {
// when finished: delete animation objects
$(this).remove();
// and attach detached items to the end of headers
detached.fadeTo(0, 0).appendTo(pivot.headers).fadeTo(200, pivot.headerOpacity);
});
}
},
setCurrentItem: function(item, index) {
let pivot = this;
let currentHeight = pivot.items.height();
pivot.items.height(currentHeight);
// hide current item immediately
pivot.items.children('.pivotItem.current').hide().removeClass('current');
// after a little delay
setTimeout(function() {
// move the item to far right and make it visible
item.css({ marginLeft: item.outerWidth() }).show().addClass('current');
// animate it to left
item.animate({ marginLeft: 0 }, pivot.animationDuration, function() {
pivot.items.height('auto');
pivot.currentItemChanged(index);
});
}, 200);
},
currentItemChanged: function(index) {
this.animating = false;
if (this.selectedItemChanged != undefined) {
this.selectedItemChanged(index);
}
},
pivotHeader_Click: function(me) {
// ignore if already current
if (me.is('.current')) return;
// ignore if still animating
if (this.animating) return;
this.animating = true;
// set current header
this.setCurrentHeader(me);
let index = me.attr('index');
// find and set current item
let item = this.items.children('.pivotItem:nth(' + index + ')');
this.setCurrentItem(item, index);
}
});
return this.initialize();
};
})(jQuery);