-
Notifications
You must be signed in to change notification settings - Fork 0
/
plunge.js
143 lines (129 loc) · 5.55 KB
/
plunge.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
/*
* Plunge.js
* v0.2.7
*/
;(function () {
'use strict'
var assignHandlers = function () {
if (!document.querySelector('[data-pl-trigger]')) {
return
}
document.querySelector('body').addEventListener('click', dropdown.open, false)
}
var findAncestor = function (el, cls) {
if (el.classList.contains(cls)) {
return el
}
while ((el = el.parentElement) && !el.classList.contains(cls)) {
// do nothing, we just want to loop through parents
}
return el
}
var findDropElement = function (el) {
var element = document.querySelectorAll('[data-pl-id="' + el + '"]')
element[0].classList.add('pl-active')
}
var escapeExit = function (event) {
if (event.which === '27') {
dropdown.closeAll()
}
}
var dropdown = {
open: function (event) {
var root = document.documentElement
var clicked = findAncestor(event.target, 'dropdown')
var dropdownEl = document.querySelector('[data-pl-id=' + event.target.dataset.plTrigger + ']')
var pos = event.target.dataset.plPosition || 'auto'
if (event.target.dataset.plTrigger && !event.target.classList.contains('pl-trigger-active')) {
event.stopPropagation()
// close all the dropdowns that might be open still
dropdown.closeAll()
// add the keyup event listener for the escape key
document.querySelector('body').addEventListener('keyup', escapeExit, false)
// add the classes to the html element
root.classList.add('pl-active')
// add an active class to the trigger
event.target.classList.add('pl-trigger-active')
// find the element that needs to plunge based on the indentifier in the data-pl-trigger attribute
findDropElement(event.target.dataset.plTrigger)
// set the position
/*
* arguments:
* - Target Element
* - Dimensions of the trigger
* - Dimensions of the dropdown Element
* - event
*/
dropdown.position[pos](dropdownEl, event.target.getBoundingClientRect(), dropdownEl.getBoundingClientRect(), event)
} else if (root.classList.contains('pl-active') && clicked === null) {
dropdown.closeAll()
} else {
return
}
},
position: {
'auto': function (dropdownEl, triggerRect, dropdownRect, event) {
var spaceBelow = window.innerHeight - (event.target.offsetTop + triggerRect.height)
var spaceRight = window.innerWidth - triggerRect.left
// default, show on the bottom
if (spaceBelow >= dropdownRect.height) {
// default align to the left
if (spaceRight >= dropdownRect.width) {
dropdown.position.bottom(dropdownEl, event.target.getBoundingClientRect(), dropdownEl.getBoundingClientRect())
} else {
// if not enough space, align to the right
dropdown.position.bottomRight(dropdownEl, event.target.getBoundingClientRect(), dropdownEl.getBoundingClientRect())
}
} else {
// default align to the left
if (spaceRight >= dropdownRect.width) {
dropdown.position.top(dropdownEl, event.target.getBoundingClientRect(), dropdownEl.getBoundingClientRect())
} else {
// if not enough space, align to the right
dropdown.position.topRight(dropdownEl, event.target.getBoundingClientRect(), dropdownEl.getBoundingClientRect())
}
}
},
'top': function (dropdownEl, triggerRect, dropdownRect, event) {
dropdownEl.style.top = (triggerRect.top - dropdownRect.height) + 'px'
dropdownEl.style.left = (triggerRect.left) + 'px'
},
'topRight': function (dropdownEl, triggerRect, dropdownRect, event) {
dropdownEl.style.top = (triggerRect.top - triggerRect.height) + 'px'
dropdownEl.style.right = (window.innerWidth - triggerRect.left - triggerRect.width) + 'px'
dropdownEl.classList.add('top-right')
},
'right': function (dropdownEl, triggerRect, dropdownRect, event) {
dropdownEl.style.top = (triggerRect.top - (dropdownRect.height / 2) + (triggerRect.height / 2)) + 'px'
dropdownEl.style.left = (triggerRect.left + triggerRect.width) + 'px'
},
'bottom': function (dropdownEl, triggerRect, dropdownRect, event) {
dropdownEl.style.top = (triggerRect.top + triggerRect.height) + 'px'
dropdownEl.style.left = (triggerRect.left) + 'px'
},
'bottomRight': function (dropdownEl, triggerRect, dropdownRect, event) {
dropdownEl.style.top = (triggerRect.top + triggerRect.height) + 'px'
dropdownEl.style.right = (window.innerWidth - triggerRect.left - triggerRect.width) + 'px'
dropdownEl.classList.add('bottom-right')
},
'left': function (dropdownEl, triggerRect, dropdownRect, event) {
dropdownEl.style.top = (triggerRect.top - (dropdownRect.height / 2) + (triggerRect.height / 2)) + 'px'
dropdownEl.style.left = (triggerRect.left - dropdownRect.width) + 'px'
}
},
close: function (element) {
element.parentNode.removeChild(element)
},
hide: function (element) {
element.classList.remove('pl-active')
element.classList.remove('pl-trigger-active')
element.blur()
},
closeAll: function () {
[].forEach.call(document.querySelectorAll('.dropdown, .pl-trigger-active'), dropdown.hide)
document.documentElement.classList.remove('pl-active', 'top-right', 'bottom-right')
document.querySelector('body').removeEventListener('keyup', escapeExit, false)
}
}
assignHandlers()
})()