2017-03-13 22:02:31 +01:00
|
|
|
var modals = (function () {
|
2017-05-05 23:43:05 +02:00
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
var active_overlay;
|
|
|
|
var close_handler;
|
|
|
|
var open_modal_name;
|
|
|
|
|
|
|
|
function reset_state() {
|
|
|
|
active_overlay = undefined;
|
|
|
|
close_handler = undefined;
|
|
|
|
open_modal_name = undefined;
|
|
|
|
}
|
2017-05-05 23:43:05 +02:00
|
|
|
|
2017-05-06 02:40:32 +02:00
|
|
|
exports.is_active = function () {
|
|
|
|
return !!open_modal_name;
|
|
|
|
};
|
|
|
|
|
2017-05-09 16:45:02 +02:00
|
|
|
exports.info_overlay_open = function () {
|
|
|
|
return open_modal_name === 'informationalOverlays';
|
|
|
|
};
|
|
|
|
|
2017-05-10 00:37:08 +02:00
|
|
|
exports.settings_open = function () {
|
|
|
|
return open_modal_name === 'settings';
|
|
|
|
};
|
|
|
|
|
2017-05-16 05:28:14 +02:00
|
|
|
exports.streams_open = function () {
|
|
|
|
return open_modal_name === 'subscriptions';
|
|
|
|
};
|
|
|
|
|
2017-05-10 15:43:36 +02:00
|
|
|
exports.lightbox_open = function () {
|
|
|
|
return open_modal_name === 'lightbox';
|
|
|
|
};
|
|
|
|
|
2017-05-06 00:37:33 +02:00
|
|
|
exports.open_overlay = function (opts) {
|
|
|
|
if (!opts.name || !opts.overlay || !opts.on_close) {
|
|
|
|
blueslip.error('Programming error in open_modal');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
if (active_overlay || open_modal_name || close_handler) {
|
|
|
|
blueslip.error('Programming error--trying to open ' + opts.name +
|
|
|
|
' before closing ' + open_modal_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-06 00:37:33 +02:00
|
|
|
// Our overlays are kind of crufty...we have an HTML id
|
|
|
|
// attribute for them and then a data-overlay attribute for
|
|
|
|
// them. Make sure they match.
|
|
|
|
if (opts.overlay.attr('data-overlay') !== opts.name) {
|
|
|
|
blueslip.error('Bad overlay setup for ' + opts.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
open_modal_name = opts.name;
|
|
|
|
active_overlay = opts.overlay;
|
2017-05-06 00:37:33 +02:00
|
|
|
opts.overlay.addClass('show');
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
close_handler = function () {
|
2017-05-06 00:37:33 +02:00
|
|
|
opts.on_close();
|
2017-05-06 02:27:40 +02:00
|
|
|
reset_state();
|
2017-05-06 00:37:33 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-05-05 23:43:05 +02:00
|
|
|
exports.close_modal = function (name) {
|
2017-05-12 17:24:16 +02:00
|
|
|
if (name !== open_modal_name) {
|
|
|
|
blueslip.error("Trying to close " + name + " when " + open_modal_name + " is open." );
|
2017-05-06 02:27:40 +02:00
|
|
|
return;
|
2017-05-05 23:43:05 +02:00
|
|
|
}
|
2017-05-06 02:27:40 +02:00
|
|
|
|
|
|
|
active_overlay.removeClass("show");
|
|
|
|
|
2017-05-12 17:24:16 +02:00
|
|
|
if (!close_handler) {
|
|
|
|
blueslip.error("Modal close handler for " + name + " not properly setup." );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
close_handler();
|
|
|
|
};
|
|
|
|
|
2017-05-10 00:37:28 +02:00
|
|
|
exports.close_active = function () {
|
|
|
|
if (!open_modal_name) {
|
|
|
|
blueslip.warn('close_active() called without checking is_active()');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.close_modal(open_modal_name);
|
|
|
|
};
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
exports.close_for_hash_change = function () {
|
|
|
|
$(".overlay.show").removeClass("show");
|
|
|
|
reset_state();
|
2017-05-05 23:43:05 +02:00
|
|
|
};
|
|
|
|
|
2017-05-06 01:04:45 +02:00
|
|
|
exports.open_settings = function () {
|
|
|
|
modals.open_overlay({
|
|
|
|
name: 'settings',
|
|
|
|
overlay: $("#settings_overlay_container"),
|
|
|
|
on_close: function () {
|
|
|
|
hashchange.exit_modal();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-05-05 23:43:05 +02:00
|
|
|
$(function () {
|
|
|
|
$("body").on("click", ".overlay, .overlay .exit", function (e) {
|
|
|
|
var $target = $(e.target);
|
|
|
|
|
|
|
|
// if the target is not the .overlay element, search up the node tree
|
|
|
|
// until it is found.
|
|
|
|
if ($target.is(".exit, .exit-sign, .overlay-content")) {
|
|
|
|
$target = $target.closest("[data-overlay]");
|
|
|
|
} else if (!$target.is(".overlay")) {
|
|
|
|
// not a valid click target then.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var target_name = $target.attr("data-overlay");
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
exports.close_modal(target_name);
|
2017-05-05 23:43:05 +02:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-03-13 22:02:31 +01:00
|
|
|
});
|
2017-05-05 23:43:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
2017-03-13 22:02:31 +01:00
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = modals;
|
|
|
|
}
|