forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog.js
More file actions
246 lines (205 loc) · 7.97 KB
/
dialog.js
File metadata and controls
246 lines (205 loc) · 7.97 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import $ from 'jquery';
import debounce from 'lodash/debounce';
/**
* Adjust the maximum size of the popup's inner scroll area so that the whole popup
* will fit within the browser viewport.
* @param {string} scrollableElementSelector - jQuery selector string for scrollable inner
* element
*/
function sizeDialogToViewport(scrollableElementSelector) {
var viewportHeight = $(window).height();
var modalDialog = $('.auto-resize-scrollable').filter(':visible');
var scrollableElement = modalDialog.find(scrollableElementSelector);
if (scrollableElement.is('iframe')) {
scrollableElement.css('height', '');
} else {
scrollableElement.css('max-height', '');
}
var dialogSize = modalDialog.offset().top + modalDialog.height();
var desiredSize = viewportHeight -
parseInt(modalDialog.css('padding-bottom'), 10) -
parseInt(modalDialog.css('margin-bottom'), 10);
var overflow = dialogSize - desiredSize;
var scrollableElementHeight = scrollableElement.height() - overflow;
scrollableElement.css('max-height', scrollableElementHeight);
if (scrollableElement.is('iframe')) {
scrollableElement.css('height', scrollableElementHeight);
} else {
scrollableElement.css('max-height', scrollableElementHeight);
}
}
/**
* Create a custom modal dialog box which takes a configurable options object.
* Currently supported options include:
* 'header' and 'body': DOM elements
* 'redirect': redirect page after the dialog is dismissed (default: no redirect)
* 'id': id of the dialog (default: none)
* 'close': whether to show a close 'x' button (default: true)
* 'width': custom width, to override CSS-specified default
* 'autoResizeScrollableElement': if selected, makes the specified selector's
* element scrollable and auto-resizes dialog to window's dimensions
*/
var Dialog = module.exports = function (options) {
// Cache visibility to avoid expensive lookup during debounced window resizing
this.isVisible = true;
var body = options.body;
var header = options.header;
var close = options.close === undefined ? true : options.close;
var closeLink = $('<div id="x-close"/>')
.addClass('x-close')
.attr('data-dismiss', 'modal');
this.div = $('<div tabindex="-1"/>').addClass('modal');
if (options.width) {
this.div.css({
width: `${options.width}px`,
marginLeft: `-${options.width / 2}px`
});
}
this.div.addClass('dash_modal');
if (options.id) {
this.div.attr('id', options.id);
}
var modalBody = $('<div/>').addClass('modal-body');
modalBody.addClass('dash_modal_body');
if (header) {
var modalHeader = $('<div/>').addClass('modal-header')
.append(header);
if (close) {
modalHeader.append(closeLink);
}
this.div.append(modalHeader);
} else if (close) {
modalBody.append(closeLink);
}
modalBody.append(body);
this.div.append(modalBody).appendTo($(document.body));
var resizeCallback;
if (options.autoResizeScrollableElement) {
this.div.addClass('auto-resize-scrollable');
var scrollableElement = this.div.find(options.autoResizeScrollableElement);
scrollableElement.css('overflow-y', 'auto');
resizeCallback = debounce(function () {
if (!this.isVisible) {
return;
}
sizeDialogToViewport(options.autoResizeScrollableElement);
}.bind(this), 250);
this.div.find('img').load(resizeCallback);
$(window).on('resize', resizeCallback);
resizeCallback();
}
// When the dialog is hidden, unhook the keydown event handler.
// If onHidden option is passed in, call that as well.
// If redirect option is passed in, redirect the page.
// After that, close the dialog.
var thisDialog = this;
$(this.div).on(
'hidden.bs.modal',
function () {
if (resizeCallback) {
thisDialog.isVisible = false;
$(window).off('resize', resizeCallback);
}
if (options.onKeydown) {
$(this.div).off('keydown', options.onKeydown);
}
if (options.onHidden) {
options.onHidden();
}
if (options.redirect) {
window.location.href = options.redirect;
}
$(this).remove();
});
$(this.div).on(
'hide.bs.modal',
function (e) {
if (this.hideOptions) {
// Let's have the dialog object handle hide options.
this.processHideOptions(this.hideOptions);
// Tell bootstrap modal dialog system not to remove this dialog yet.
e.preventDefault();
// Remove the options from dialog object so that when this event is called again at the
// end of the processing of hide options, we will just let bootstrap's modal dialog
// system clean it up like normal.
this.hideOptions = null;
}
}.bind(this));
if (options.onKeydown) {
$(this.div).on('keydown', options.onKeydown);
}
};
/**
* Options is configurable with a top and left properties, both are integers.
* Also includes staticBackdrop. When true, modal dialog's backdrop will not
* close the dialog when clicked.
* The caller can also specify hideOptions, for special behavior when the dialog is dismissed.
*/
Dialog.prototype.show = function (options) {
options = options || {};
$(this.div).modal({
show: true,
// The default value for backdrop is true, meaning clicking the backdrop
// will close the modal. A value of 'static' will not close the modal.
backdrop: options.backdrop || true
});
this.isVisible = true;
// Store hideOptions for later use when the dialog is dismissed, inside the div itself.
if (options.hideOptions) {
this.hideOptions = options.hideOptions;
}
this.div.offset(options);
};
Dialog.prototype.hide = function () {
$(this.div).modal('hide');
};
Dialog.prototype.focus = function () {
if (this.isVisible) {
$(this.div).focus();
}
};
/**
* This processes optional hideOptions that were provided to show().
* At the moment it will play an animation of the dialog moving and resizing to
* the location and dimensions of a specified div, while also fading out.
* Certain elements are faded out more quickly so that they are gone before
* the dialog gets too small.
*/
Dialog.prototype.processHideOptions = function (options) {
var startCss = {};
startCss.opacity = '1';
startCss.left = this.div.css('left');
startCss.top = this.div.css('top');
startCss.width = this.div.css('width');
startCss.height = this.div.css('height');
// The dialog current is 640px wide but has a left margin of -320px. We need to
// compensate for that when determining where the animation should end at.
var marginLeft = parseInt(this.div.css("marginLeft"));
var endCss = {};
endCss.opacity = '0';
endCss.overflow = "visible";
endCss.left = $(options.endTarget).offset().left - marginLeft;
endCss.top = $(options.endTarget).offset().top - $(window).scrollTop();
endCss.width = $(options.endTarget).css("width");
endCss.height = $(options.endTarget).css("height");
// Fade the whole thing out slowly.
var totalFadeTime = 500;
// Fade some of the decorative elements quickly.
var decorationFadeTime = 150;
// Fade some elements really fast.
var fastFadeTime = 50;
// Let's also fade the background out.
$(".modal-backdrop").animate({opacity: 0}, totalFadeTime);
// And a bunch of other elements
this.div.find(".farSide").animate({opacity: 0}, decorationFadeTime);
this.div.find("#x-close").animate({opacity: 0}, decorationFadeTime);
this.div.find(".dialog-title").animate({opacity: 0}, decorationFadeTime);
this.div.find(".aniGif").animate({opacity: 0}, decorationFadeTime);
this.div.find(".modal-content p").animate({"font-size": "13px"}, totalFadeTime);
this.div.find(".markdown-instructions-container").animate({opacity: 0}, fastFadeTime);
// Slide the instruction box from its current position to its destination.
$(this.div).css(startCss).animate(endCss, totalFadeTime, "swing", function () {
// Just hide the dialog at the end.
$(this).modal('hide');
});
};