 
 uploadcare.namespace '', (ns) ->
 
+  lockDialogFocus = (e) ->
+    if !e.shiftKey and focusableElements.last().is(e.target)
+      e.preventDefault()
+      focusableElements.first().focus()
+    else if e.shiftKey and focusableElements.first().is(e.target)
+      e.preventDefault()
+      focusableElements.last().focus()
+
   lockScroll = (el, toTop) ->
     top = el.scrollTop()
     left = el.scrollLeft()

      currentDialogPr = null
      dialog.remove()
      cancelLock()


  ns.openPreviewDialog = (file, settings) ->

    dialog.addClass('uploadcare--dialog_status_active')
    dialogPr.dialogElement = dialog

    focusableElements = dialog.find('select, input, textarea, button, a[href]')
    focusableElements.first().focus()

    cancelLock = lockScroll($(window), dialog.css('position') is 'absolute')
    $('html, body').addClass(openedClass)

