/
modal.js
172 lines (162 loc) · 5.36 KB
/
modal.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
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
import jQuery from "jquery";
// ============================================================================
//TODO: (the older version) unify with ui-modal (the newer version)
var $ = jQuery;
// Modal dialog boxes
var Modal = function(options) {
this.$overlay = options.overlay;
this.$dialog = options.dialog;
this.$header = this.$dialog.find(".modal-header");
this.$body = this.$dialog.find(".modal-body");
this.$footer = this.$dialog.find(".modal-footer");
this.$backdrop = options.backdrop;
// Close button
this.$header.find(".close").on("click", $.proxy(this.hide, this));
};
$.extend(Modal.prototype, {
setContent: function(options) {
this.$header.hide();
// Title
if (options.title) {
this.$header.find(".title").html(options.title);
this.$header.show();
}
if (options.closeButton) {
this.$header.find(".close").show();
this.$header.show();
} else {
this.$header.find(".close").hide();
}
// Buttons
this.$footer.hide();
var $buttons = this.$footer.find(".buttons").html("");
if (options.buttons) {
$.each(options.buttons, (name, value) => {
$buttons
.append(
$("<button></button> ")
.text(name)
.click(value)
)
.append(" ");
});
this.$footer.show();
}
var $extraButtons = this.$footer.find(".extra_buttons").html("");
if (options.extra_buttons) {
$.each(options.extra_buttons, (name, value) => {
$extraButtons
.append(
$("<button></button>")
.text(name)
.click(value)
)
.append(" ");
});
this.$footer.show();
}
// Body
var body = options.body;
if (body == "progress") {
body = $(
"<div class='progress progress-striped active'><div class='progress-bar' style='width: 100%'></div></div>"
);
}
this.$body.html(body);
},
show: function(options, callback) {
if (!this.$dialog.is(":visible")) {
if (options.backdrop) {
this.$backdrop.addClass("in");
} else {
this.$backdrop.removeClass("in");
}
this.$overlay.show();
this.$dialog.show();
this.$overlay.addClass("in");
// Fix min-width so that modal cannot shrink considerably if new content is loaded.
this.$body.css("min-width", this.$body.width());
// Set max-height so that modal does not exceed window size and is in middle of page.
// TODO: this could perhaps be handled better using CSS.
this.$body.css(
"max-height",
$(window).height() -
this.$footer.outerHeight() -
this.$header.outerHeight() -
parseInt(this.$dialog.css("padding-top"), 10) -
parseInt(this.$dialog.css("padding-bottom"), 10)
);
}
// Callback on init
if (callback) {
callback();
}
},
hide: function() {
var modal = this;
modal.$dialog.fadeOut(() => {
modal.$overlay.hide();
modal.$backdrop.removeClass("in");
modal.$body.children().remove();
// Clear min-width to allow for modal to take size of new body.
modal.$body.css("min-width", undefined);
});
}
});
var modal;
$(() => {
modal = new Modal({
overlay: $("#top-modal"),
dialog: $("#top-modal-dialog"),
backdrop: $("#top-modal-backdrop")
});
});
// Backward compatibility
function hide_modal() {
modal.hide();
}
function show_modal(title, body, buttons, extra_buttons, init_fn) {
modal.setContent({
title: title,
body: body,
buttons: buttons,
extra_buttons: extra_buttons
});
modal.show({ backdrop: true }, init_fn);
}
function show_message(title, body, buttons, extra_buttons, init_fn) {
modal.setContent({
title: title,
body: body,
buttons: buttons,
extra_buttons: extra_buttons
});
modal.show({ backdrop: false }, init_fn);
}
function show_in_overlay(options) {
var width = options.width || "600";
var height = options.height || "400";
var scroll = options.scroll || "auto";
$("#overlay-background").bind("click.overlay", () => {
hide_modal();
$("#overlay-background").unbind("click.overlay");
});
modal.setContent({
closeButton: true,
title: " ",
body: $(
`<div style='margin: -5px;'><iframe style='margin: 0; padding: 0;' src='${
options.url
}' width='${width}' height='${height}' scrolling='${scroll}' frameborder='0'></iframe></div>`
)
});
modal.show({ backdrop: true });
}
// ============================================================================
export default {
Modal: Modal,
hide_modal: hide_modal,
show_modal: show_modal,
show_message: show_message,
show_in_overlay: show_in_overlay
};