UNPKG

alertifyjs

Version:

AlertifyJS is a javascript framework for developing pretty browser dialogs and notifications.

201 lines (176 loc) 7.53 kB
/** * Bind dialogs events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function bindEvents(instance) { // if first dialog, hook global handlers if (openDialogs.length === 1) { //global on(window, 'resize', windowResize); on(document.body, 'keyup', keyupHandler); on(document.body, 'keydown', keydownHandler); on(document.body, 'focus', onReset); //move on(document.documentElement, 'mousemove', move); on(document.documentElement, 'touchmove', move, false, false); on(document.documentElement, 'mouseup', endMove); on(document.documentElement, 'touchend', endMove); //resize on(document.documentElement, 'mousemove', resize); on(document.documentElement, 'touchmove', resize, false, false); on(document.documentElement, 'mouseup', endResize); on(document.documentElement, 'touchend', endResize); } // common events on(instance.elements.commands.container, 'click', instance.__internal.commandsClickHandler); on(instance.elements.footer, 'click', instance.__internal.buttonsClickHandler); on(instance.elements.reset[0], 'focusin', instance.__internal.resetHandler); on(instance.elements.reset[0], 'keydown', recycleTab); on(instance.elements.reset[1], 'focusin', instance.__internal.resetHandler); //prevent handling key up when dialog is being opened by a key stroke. cancelKeyup = true; // hook in transition handler on(instance.elements.dialog, transition.type, instance.__internal.transitionInHandler); // modelss only events if (!instance.get('modal')) { bindModelessEvents(instance); } // resizable if (instance.get('resizable')) { bindResizableEvents(instance); } // movable if (instance.get('movable')) { bindMovableEvents(instance); } } /** * Unbind dialogs events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function unbindEvents(instance) { // if last dialog, remove global handlers if (openDialogs.length === 1) { //global off(window, 'resize', windowResize); off(document.body, 'keyup', keyupHandler); off(document.body, 'keydown', keydownHandler); off(document.body, 'focus', onReset); //move off(document.documentElement, 'mousemove', move); off(document.documentElement, 'mouseup', endMove); //resize off(document.documentElement, 'mousemove', resize); off(document.documentElement, 'mouseup', endResize); } // common events off(instance.elements.commands.container, 'click', instance.__internal.commandsClickHandler); off(instance.elements.footer, 'click', instance.__internal.buttonsClickHandler); off(instance.elements.reset[0], 'focusin', instance.__internal.resetHandler); off(instance.elements.reset[0], 'keydown', recycleTab); off(instance.elements.reset[1], 'focusin', instance.__internal.resetHandler); // hook out transition handler on(instance.elements.dialog, transition.type, instance.__internal.transitionOutHandler); // modelss only events if (!instance.get('modal')) { unbindModelessEvents(instance); } // movable if (instance.get('movable')) { unbindMovableEvents(instance); } // resizable if (instance.get('resizable')) { unbindResizableEvents(instance); } } /** * Bind modeless specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function bindModelessEvents(instance) { on(instance.elements.dialog, 'focus', instance.__internal.bringToFrontHandler, true); } /** * Unbind modeless specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function unbindModelessEvents(instance) { off(instance.elements.dialog, 'focus', instance.__internal.bringToFrontHandler, true); } /** * Bind movable specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function bindMovableEvents(instance) { on(instance.elements.header, 'mousedown', instance.__internal.beginMoveHandler); on(instance.elements.header, 'touchstart', instance.__internal.beginMoveHandler, false, false); } /** * Unbind movable specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function unbindMovableEvents(instance) { off(instance.elements.header, 'mousedown', instance.__internal.beginMoveHandler); off(instance.elements.header, 'touchstart', instance.__internal.beginMoveHandler, false, false); } /** * Bind resizable specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function bindResizableEvents(instance) { on(instance.elements.resizeHandle, 'mousedown', instance.__internal.beginResizeHandler); on(instance.elements.resizeHandle, 'touchstart', instance.__internal.beginResizeHandler, false, false); } /** * Unbind resizable specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function unbindResizableEvents(instance) { off(instance.elements.resizeHandle, 'mousedown', instance.__internal.beginResizeHandler); off(instance.elements.resizeHandle, 'touchstart', instance.__internal.beginResizeHandler, false, false); } /** * Bind closable events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function bindClosableEvents(instance) { on(instance.elements.modal, 'click', instance.__internal.modalClickHandler); } /** * Unbind closable specific events * * @param {Object} instance The dilog instance. * * @return {undefined} */ function unbindClosableEvents(instance) { off(instance.elements.modal, 'click', instance.__internal.modalClickHandler); }