/* * #%L * synat-portal-webapp Maven Webapp * %% * Copyright (C) 2010 - 2013 ICM, Warsaw University * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ $(function(){ function showModalWindow(data){ var modal = $(this); var visibleModals = $('.modal-window:visible'); modal.trigger('beforeShow'); if(visibleModals.length == 0) { $('body').prepend('
'); } else { var maxZindex = 0; visibleModals.each(function(){ var zindex = parseInt($(this).css('z-index')); if(zindex > maxZindex){ maxZindex = zindex; } }); modal.css('z-index', maxZindex+1); } modal.show(); modal.trigger('reposition', function(){ modal.trigger('afterShow', [data]); }); bindGlobalEvents.call(modal); bindTabBlocking.call(modal); modal.find(".focus-init").first().focus(); } function closeModalWindow(){ $("body").removeClass("block_scroll"); var modal = $(this); if(!modal.is(':visible')) { return; } slideWindowFromScreen(close); function slideWindowFromScreen(callback){ if(typeof callback == "undefined"){ callback == function(){}; } modal.animate({'top' : -(1.2)*modal.height()}, 100, 'linear', callback); } function close(){ if($('.modal-window:visible').length == 1) { $('.modal-window-overlay').first().remove(); } unbindTabBlocking.call(modal); unbindGlobalEvents.call(modal); modal.hide(); modal.trigger('afterClose'); } } function resizeModalWindow(event, callback){ } function repositionModalWindow(event, callback){ var modal = $(this); callback = typeof callback == 'undefined' ? function(){} : callback; modal.position({ of: $(window), using: function(newPosition){ $(".modal-window:visible").css("height", "auto" ); if ($(window).height() < $(".modal-window:visible").height()){ currentPosition = 0; $(".modal-window:visible").css("height", $(window).height() - 10 ); $(".modal-window:visible").css("overflow-y", "scroll"); }else{ $(".modal-window:visible").css("height", "auto" ); currentPosition = newPosition.top; $(".modal-window:visible").css("overflow-y", "auto"); } modal.stop(); modal.css({'left' : newPosition.left}); modal.animate({'top' : currentPosition}, 100, 'linear', callback); } }); } function blockTabOnLastElement(){ var eventName = 'keydown.blockTab.'+this.attr('id'); var container = $('#'+this.attr('id')); var eventMap = []; eventMap[eventName] = function(event){ if(event.which == 9 && !event.shiftKey) { //Tab event.preventDefault(); } }; var selector; if(container.find('.stop-focus-last').length > 0){ selector = '.stop-focus-last'; } else { selector = '.modal-window-content :input:visible:last, '; selector += '.modal-window-content :button:visible:last'; } container.on(eventMap, selector); } function blockShiftTabOnFirstElement(){ var eventName = 'keydown.blockShiftTab.'+this.attr('id'); var eventMap = []; eventMap[eventName] = function(event){ if(event.which == 9 && event.shiftKey) { //Tab event.preventDefault(); } }; var selector = '.top a'; $('#'+this.attr('id')).on(eventMap, selector); } function bindTabBlocking(){ blockTabOnLastElement.call(this); blockShiftTabOnFirstElement.call(this); } function unbindTabBlocking(){ var tabBlockEvent = 'keydown.blockTab.'+this.attr('id'); var shiftTabBlockEvent = 'keydown.blockShiftTab.'+this.attr('id'); $('#'+this.attr('id')).off(tabBlockEvent); $('#'+this.attr('id')).off(shiftTabBlockEvent); } function bindGlobalEvents(){ var modal = this; var resizeEventName = 'resize.modal-'+modal.attr('id'); var closeModal = 'keydown.modal-'+modal.attr('id'); var windowEventMap = []; windowEventMap[resizeEventName] = function(){ modal.trigger('reposition'); }; windowEventMap[closeModal] = function(event){ if(event.which == 27){ //press ESC $(window).off(closeModal); modal.trigger('close'); } }; $(window).on(windowEventMap); } function unbindGlobalEvents(modal){ var modal = $(this); $(window).off('resize.modal-'+modal.attr('id')); $(window).off('keydown.modal-'+modal.attr('id')); } //Events binding $('body').on({ 'beforeShow' : function(){}, 'show' : function(event, data){ showModalWindow.call(this, data); $("body").addClass("block_scroll"); }, 'afterShow' : function(){ $(this).trigger('reposition'); }, 'afterClose' : function(){}, 'close' : closeModalWindow, 'resize' : resizeModalWindow, 'reposition' : repositionModalWindow }, '.modal-window'); /* $('body').on({'click': function(event){ event.preventDefault(); numberInputElement = $(".modal-window:visible input:visible, .modal-window:visible textarea:visible").size(); actualInputElement = 0 $(".modal-window:visible input:visible, .modal-window:visible textarea:visible").each(function() { actualInputElement = actualInputElement + 1; if (!($(this).val()=="")){ currentShowModal = $(".modal-window:visible").attr("id") + '_leaveUnsavedFormConfirmationModal'; showModal(currentShowModal); return false; } else { if (actualInputElement == numberInputElement) { $('.modal-window:visible').find('.button-cancel').trigger('click'); $('.modal-window:visible').trigger('close'); } } }); } }, '.modal-window-overlay'); */ $('body').on({ 'click': function(event){ event.preventDefault(); $(this).closest('.modal-window').trigger('close'); }, 'keydown': function(event) { if (event.which === 13) { event.preventDefault(); $(this).closest('.modal-window').trigger('close'); } } }, '.modal-window-close'); $('.modal-window').on('keydown.closeOnEsc', function(event){ if(event.which === 27){ $(this).trigger('close'); } }); //Global functions for managing modal windows window.showModal = function(id, data){ $('#'+id).trigger('show', [data]); } window.hideModal = function(id){ $('#'+id).trigger('close'); } })