forked from Viewtiful/fs-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-dialog-service.js
110 lines (98 loc) · 3.3 KB
/
fs-dialog-service.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
// service to handle stacking of dialogs
(function () {
var dialogStack = [];
var buffer = [];
var bufferElements = (document.readyState === 'loading');
window.FS = window.FS || {};
FS.dialog = FS.dialog || {};
FS.dialog.service = FS.dialog.service || {};
/**
* Register global elements and attach a reference to them to FS.dialog.
* @param {string} elementName - The name of the element to register e.g. fs-person-card
* @returns {undefined} - Returns void.
*/
FS.dialog.register = function (elementName) {
if (bufferElements) {
buffer.push(elementName);
return;
}
registerElement(elementName);
};
function registerElement (elementName) {
var camelCaseName = elementName.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
if (FS.dialog[camelCaseName]) {
console.error('Attempted to create element', elementName, 'which already exists');
return;
}
var element = document.createElement(elementName);
document.body.appendChild(element);
Object.defineProperty(FS.dialog, camelCaseName, {
get: function () {
return element;
}
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
bufferElements = false;
buffer.forEach(registerElement);
});
}
FS.dialog.service.addDialogToStack = function (dialogElement) {
FS.dialog.service.removeDialogFromStack(dialogElement);
if (dialogElement) {
dialogStack.push(dialogElement);
}
};
FS.dialog.service.removeDialogFromStack = function (dialogElement) {
var index = dialogStack.indexOf(dialogElement);
if (index > -1) dialogStack.splice(index, 1);
};
FS.dialog.service.dialogIsOnTop = function (dialogElement) {
var index = dialogStack.indexOf(dialogElement);
var lastIndex = dialogStack.length - 1;
return index === lastIndex;
};
FS.dialog.service.closeAllDialogs = function () {
var reverseStack = [].concat(dialogStack).reverse();
reverseStack.forEach(function (dialog) {
if (!dialog) {
FS.dialog.service.removeDialogFromStack(dialog);
} else if (dialog.close) {
dialog.close();
}
});
};
FS.dialog.service.closeDialogAndAllChildren = function (dialogElement) {
var reverseStack = [].concat(dialogStack).reverse();
var index = reverseStack.indexOf(dialogElement);
var animationToUseToClose = dialogElement.getAttribute('transition');
reverseStack.some(function (dialog, dialogIndex) {
if (!dialog) {
FS.dialog.service.removeDialogFromStack(dialog);
return false;
}
if (dialogIndex <= index) {
var animationToRestore = dialog.getAttribute('transition');
if (animationToUseToClose) {
dialog.setAttribute('transition', animationToUseToClose);
}
if (dialog.close) {
dialog.close();
}
// Restsore the animation direction after the transition has finished
if (animationToUseToClose) {
setTimeout(function () {
dialog.setAttribute('transition', animationToRestore);
}, 300);
}
} else {
return true;
}
});
};
FS.dialog.service.getStack = function () {
return dialogStack;
};
FS.dialog.service.windowHasFocus = true;
})();