1 |
2
|
Manuela
|
|
2 |
|
|
"use strict";
|
3 |
|
|
|
4 |
|
|
var Modal = function(){
|
5 |
|
|
|
6 |
|
|
var modalOverlay = document.createElement('div');
|
7 |
|
|
modalOverlay.setAttribute('class', 'modal-overlay');
|
8 |
|
|
document.body.appendChild(modalOverlay);
|
9 |
|
|
|
10 |
|
|
var config = {
|
11 |
|
|
$modal: document.querySelector('.modal'),
|
12 |
|
|
$modalOverlay: document.querySelector('.modal-overlay'),
|
13 |
|
|
modalOverlayColor: 'rgba(255,255,255,0.7)',
|
14 |
|
|
$modalClose: document.querySelector('.modal-close'),
|
15 |
|
|
$modalTrigger: document.querySelector('.modal-trigger'),
|
16 |
|
|
$draggie: new Draggabilly( document.querySelector('.modal'), {
|
17 |
|
|
handle: '.modal-header',
|
18 |
|
|
containment: 'html'
|
19 |
|
|
})
|
20 |
|
|
}
|
21 |
|
|
$modal.$modalOverlay.style.background = config.modalOverlayColor;
|
22 |
|
|
|
23 |
|
|
var Modal = {
|
24 |
|
|
createEvent: function(eventName, callback){
|
25 |
|
|
var event = document.createEvent('HTMLEvents');
|
26 |
|
|
event.initEvent(eventName, true, false);
|
27 |
|
|
config.$modal.dispatchEvent(event);
|
28 |
|
|
|
29 |
|
|
if (callback && typeof(callback) === "function") {
|
30 |
|
|
callback();
|
31 |
|
|
}
|
32 |
|
|
},
|
33 |
|
|
open: function(){
|
34 |
|
|
config.$modal.style.visibility = "visible";
|
35 |
|
|
config.$modal.classList.add('opening');
|
36 |
|
|
config.$modal.classList.remove('closing');
|
37 |
|
|
config.$modalOverlay.style.visibility = "visible";
|
38 |
|
|
config.$modalOverlay.classList.add('opening');
|
39 |
|
|
config.$modalOverlay.classList.remove('closing');
|
40 |
|
|
Modal.createEvent('modalOpened', function(){
|
41 |
|
|
//callback
|
42 |
|
|
})
|
43 |
|
|
},
|
44 |
|
|
close: function(){
|
45 |
|
|
config.$modal.classList.add('closing');
|
46 |
|
|
config.$modal.classList.remove('opening');
|
47 |
|
|
config.$modalOverlay.classList.add('closing');
|
48 |
|
|
config.$modalOverlay.classList.remove('opening');
|
49 |
|
|
var timer = window.setTimeout(function(){
|
50 |
|
|
config.$modal.style.visibility = "hidden";
|
51 |
|
|
config.$modalOverlay.style.visibility = "hidden";
|
52 |
|
|
Modal.createEvent('modalClosed', function(){
|
53 |
|
|
//callback
|
54 |
|
|
});
|
55 |
|
|
},500);
|
56 |
|
|
},
|
57 |
|
|
init: function(){
|
58 |
|
|
config.$draggie.on( 'dragStart', function( instance, event, pointer ){
|
59 |
|
|
console.log( 'dragMove on ' + event.type + pointer.pageX + ', ' + pointer.pageY + ' position at ' + instance.position.x + ', ' + instance.position.y );
|
60 |
|
|
});
|
61 |
|
|
|
62 |
|
|
config.$modalClose.addEventListener("click", function(){
|
63 |
|
|
Modal.close();
|
64 |
|
|
}, false);
|
65 |
|
|
|
66 |
|
|
config.$modalTrigger.addEventListener("click", function(){
|
67 |
|
|
Modal.open();
|
68 |
|
|
}, false);
|
69 |
|
|
|
70 |
|
|
window.addEventListener("keydown", function(e){
|
71 |
|
|
console.log(e.keyCode);
|
72 |
|
|
if (e.keyCode == 27) {
|
73 |
|
|
if( config.$modal.style.display != 'none'){
|
74 |
|
|
Modal.close();
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
if (e.keyCode == 38 && e.ctrlKey) {
|
79 |
|
|
console.log('full');
|
80 |
|
|
if(config.$modal.classList.contains("is-right") || config.$modal.classList.contains("is-left")){
|
81 |
|
|
config.$modal.classList.remove("is-right");
|
82 |
|
|
config.$modal.classList.remove("is-left");
|
83 |
|
|
config.$draggie.enable();
|
84 |
|
|
} else {
|
85 |
|
|
config.$draggie.disable();
|
86 |
|
|
config.$modal.classList.add("is-full");
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
if (e.keyCode == 37 && e.ctrlKey) {
|
91 |
|
|
console.log('left');
|
92 |
|
|
if(config.$modal.classList.contains("is-right") || config.$modal.classList.contains("is-full")){
|
93 |
|
|
config.$modal.classList.remove("is-right");
|
94 |
|
|
config.$modal.classList.remove("is-full");
|
95 |
|
|
config.$draggie.enable();
|
96 |
|
|
} else {
|
97 |
|
|
config.$modal.classList.add("is-left");
|
98 |
|
|
config.$draggie.disable();
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
if (e.keyCode == 39 && e.ctrlKey) {
|
103 |
|
|
console.log('right');
|
104 |
|
|
if(config.$modal.classList.contains("is-left") || config.$modal.classList.contains("is-full")){
|
105 |
|
|
config.$modal.classList.remove("is-left");
|
106 |
|
|
config.$modal.classList.remove("is-full");
|
107 |
|
|
config.$draggie.enable();
|
108 |
|
|
} else {
|
109 |
|
|
config.$modal.classList.add("is-right");
|
110 |
|
|
config.$draggie.disable();
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
if (e.keyCode == 40 && e.ctrlKey) {
|
115 |
|
|
if(config.$modal.classList.contains("is-full")){
|
116 |
|
|
config.$modal.classList.remove("is-full");
|
117 |
|
|
config.$draggie.enable();
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
});
|
122 |
|
|
|
123 |
|
|
config.$modal.addEventListener("modalOpened", function(){
|
124 |
|
|
console.log("Modal Opened");
|
125 |
|
|
})
|
126 |
|
|
config.$modal.addEventListener("modalClosed", function(){
|
127 |
|
|
console.log("Modal Closed");
|
128 |
|
|
})
|
129 |
|
|
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
Modal.config = config;
|
133 |
|
|
|
134 |
|
|
return Modal;
|
135 |
|
|
|
136 |
|
|
};
|