/**
* Created by Abdulnasyrova on 19.04.2017.
*/
var KEY_CODES = {
ENTER: 13,
TAB: 9,
ESC: 27,
SPACE: 32,
LEFT: 37,
RIGHT: 39
};
var Standard_Dialog = function() {
var $manuscript = $('#manuscript');
var $body = $('body');
var $dialog, $content;
var options = {};
return {
open: function(html, opt) {
// Extend default options
options = $.extend(true, (opt || {}), {
width: 450,
modal: true,
draggable: false,
resizable: false
});
// Set buttons
if (options.buttons === 'none') {
options.buttons = null;
} else if (options.buttons === 'OK' || !options.buttons) {
options.buttons = [{text: 'OK', click: Standard_Dialog.close}];
}
// Remove previous dialog
$('#standard_dialog_div').dialog('close').remove();
if (options.modal) {
$body.css('overflow', 'hidden');
}
// Hide other content from reader for mobile devices
$manuscript.attr('aria-hidden', 'true');
$dialog = $('
');
$content = $('' + (html || '') + '
');
$dialog.append($content);
$dialog.dialog(options);
// If we open toc we use as description only instruction in the top of dialog and not links
var isMenu = $content.find('#toc_head').length;
var labelledBy = isMenu ? 'toc_head' : 'standard_dialog_div';
var $parentDialog = $dialog.parent().attr({
'tabindex': '0',
'role': 'dialog',
'aria-labelledby': 'ui-dialog-title-standard_dialog_div ' + labelledBy
});
$parentDialog.find('#ui-dialog-title-standard_dialog_div').attr({
'role': 'heading',
'aria-level': '1'
});
// Remove close btn
$('.ui-dialog-titlebar-close').remove();
var $lastBtn = $parentDialog.find('button').last();
var $firstBtn = $parentDialog.find('button').first();
// Attach events
$parentDialog.bind('keydown', function(e) {
var code = e.keyCode || e.which;
if (code === KEY_CODES.ESC) {
Standard_Dialog.close(true);
}
if (e.target === this && e.shiftKey && code === KEY_CODES.TAB) {
e.preventDefault();
$lastBtn.focus();
}
});
$lastBtn.bind('keydown', function(e) {
var code = e.keyCode || e.which;
if (!e.shiftKey && code === KEY_CODES.TAB) {
$parentDialog.focus();
e.preventDefault();
}
});
if (platform.mobile) {
// For android we set focus on dialog in any cases
$parentDialog.focus();
} else if (isMenu) {
// If it is menu put focus on first link
$parentDialog.find('a').first().focus();
} else {
// For other platform we set focus on first button
$firstBtn.focus();
}
},
wait: function(html, opt) {
options = opt || {};
options.buttons = 'none';
Standard_Dialog.open(html, options);
},
alert: function(html, opt) {
Standard_Dialog.open(html, opt);
},
close: function() {
// Show other content
$manuscript.removeAttr('aria-hidden');
$dialog.dialog('close').remove();
if (options.from) {
options.from.focus();
}
if (options.modal) {
$body.css('overflow', 'auto');
}
}
};
}();