-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
item.js
91 lines (79 loc) · 2.57 KB
/
item.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
import * as utils from '../common/utils.js';
import { getOptions } from '../common/options.js';
import { state } from '../common/state.js';
import {
ACTIVE,
SLIDES_CONTAINER_SEL,
WRAPPER_SEL
} from '../common/selectors.js';
export const plainItem = function(panel){
this.anchor = panel.anchor;
this.item = panel.item;
this.index = panel.index();
this.isLast = this.index === panel.item.parentElement.querySelectorAll(panel.selector).length -1;
this.isFirst = !this.index;
this.isActive = panel.isActive;
};
/**
* Item. Slide or Section objects share the same properties.
*/
export const Item = function(el, selector){
this.parent = this.parent || null;
this.selector = selector;
this.anchor = utils.getAttr(el, 'data-anchor') || getOptions().anchors[utils.index(el, getOptions().sectionSelector)];
this.item = el;
this.isVisible = utils.isVisible(el);
this.isActive = utils.hasClass(el, ACTIVE);
this.hasScroll = utils.hasClass(el, 'fp-overflow');
this.isSection = selector === getOptions().sectionSelector;
this.container = utils.closest(el, SLIDES_CONTAINER_SEL) || utils.closest(el, WRAPPER_SEL);
this.index = function(){
if(this.isSection){
return state.sections.indexOf(this);
}else if(this.parent && this.parent.slides){
return this.parent.slides.indexOf(this);
}
return 0;
};
};
Item.prototype.siblings = function(){
return this.isSection ? state.sections : this.parent.slides;
};
Item.prototype.prev = function(){
var siblings = this.siblings();
var currentIndex = this.isSection ? siblings.indexOf(this) : this.parent.slides.indexOf(this);
var prevIndex = currentIndex - 1;
if(prevIndex >= 0){
return siblings[prevIndex];
}
return null;
};
Item.prototype.next = function(){
var siblings = this.siblings();
var currentIndex = this.isSection ? siblings.indexOf(this) : this.parent.slides.indexOf(this);
var nextIndex = currentIndex + 1;
if(nextIndex < siblings.length){
return siblings[nextIndex];
}
return null;
};
Item.prototype.getSiblings = function(){
if(this.isSection){
return state.sections;
}
return state.panels;
};
export function getNodes(panels){
return panels.map(panel => panel.item);
}
export function getPanelByElement(panels, el){
return panels.find(function(panel){
return panel.item === el;
});
}
export const Section = function(el){
plainItem.call(this, el);
};
export const Slide = function(el){
plainItem.call(this, el);
};