Explorar o código

Fix form submission problem after offline/online change

Markus Ochel %!s(int64=12) %!d(string=hai) anos
pai
achega
4537db3e9a

+ 6 - 1
admin/controllers/authors.coffee

@@ -83,6 +83,10 @@ class AuthorForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -111,7 +115,7 @@ class AuthorForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -132,6 +136,7 @@ class AuthorForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 6 - 1
admin/controllers/blocks.coffee

@@ -92,6 +92,10 @@ class BlockForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -115,7 +119,7 @@ class BlockForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -136,6 +140,7 @@ class BlockForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 6 - 1
admin/controllers/collections.coffee

@@ -103,6 +103,10 @@ class CollectionForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -126,7 +130,7 @@ class CollectionForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -147,6 +151,7 @@ class CollectionForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 6 - 1
admin/controllers/contacts.coffee

@@ -50,6 +50,10 @@ class ContactForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -68,7 +72,7 @@ class ContactForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -89,6 +93,7 @@ class ContactForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 6 - 1
admin/controllers/essays.coffee

@@ -194,6 +194,10 @@ class EssayForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+
     if @editing
       @item.fromForm(@form)
     else
@@ -218,7 +222,7 @@ class EssayForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -239,6 +243,7 @@ class EssayForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 19 - 8
admin/controllers/filter-box.coffee

@@ -21,6 +21,8 @@ class FilterBox extends Spine.Controller
     context = 
       sites: Site.all()
     @html templates.render('filter-box.html', {}, context)
+    @siteId = ''
+    @query = ''
     @setup()
     @
 
@@ -50,20 +52,28 @@ class FilterBox extends Spine.Controller
     
     @siteSelector.find('li').on 'click', (e) =>
       $item = $(e.currentTarget)
-      @selectedSite.find('> span').html($item.html()).attr('class', $item.attr('class'))
-      @siteId = $item.attr('data-id')
-      @siteSelector
-        .hide()
-        .scrollTop(0)
-        .children()
-        .removeClass('selected')
-      $item.addClass('selected')
+      @changeSite($item)
       @filter()
 
+  changeSite: ($item) ->
+    @selectedSite.find('> span').html($item.html()).attr('class', $item.attr('class'))
+    @siteId = $item.attr('data-id')
+    @siteSelector
+      .hide()
+      .scrollTop(0)
+      .children()
+      .removeClass('selected')
+    $item.addClass('selected')
+
   clear: ->
     @filterInput.val('')
     @filter()
 
+  reset: =>
+    $item = @siteSelector.children().first()
+    @changeSite($item)
+    @clear()
+
   filter: =>
     @query = $.trim(@filterInput.val())
     if @query
@@ -75,4 +85,5 @@ class FilterBox extends Spine.Controller
       siteId: @siteId
 
 
+
 module.exports = FilterBox

+ 20 - 22
admin/controllers/index.coffee

@@ -24,58 +24,59 @@ class App extends Spine.Controller
   
   constructor: ->
     super
+    @appStarted = false
     @mainNav = new MainNav
     @append @mainNav
     @initApp()
 
   initApp: =>
-    @setupSession()
-
     @mainStack = new MainStack
     @helpUI    = new HelpUI
+    @append @mainStack, @helpUI
     Spine.Route.setup()
-    @navigate('/')
-
+    
+    @setupSession()
     @hookPanelsToNav()
     @setupOnlineOffline()
     @doOtherStuff()
 
   setupSession: ->
     session.on 'change', @checkRole
-    $(window).on 'focus', @getSessionInfo
-    # @mainNav.bind 'beforeChange', @getSessionInfo
     @getSessionInfo()
 
   getSessionInfo: =>
     session.info (err, info) =>
-      if info
+      if not info
+        # try again in 5 seconds
+        @mainNav.offline.show()
+        @delay @getSessionInfo, 5000
+      else
         @mainNav.offline.hide()
         @checkRole info.userCtx
-      else
-        @mainNav.offline.show()
 
   checkRole: (userCtx) =>
-    if 'manager' in userCtx.roles
+    if userCtx?.roles and 'manager' in userCtx.roles
       @mainNav.hideLogin()
-      @startApp() unless @appStarted
-      @loadData() unless @dataLoaded
       @mainNav.greetUser(userCtx.name)
+      @startApp()
+      @mainStack.el.css('visibility', 'visible')
+      @helpUI.el.css('visibility', 'visible')
     else
       @mainNav.showLogin()
-      @unloadData() if @dataLoaded
-      @endApp() if @appStarted
+      @endApp()
+      @mainStack.el.css('visibility', 'hidden')
+      @helpUI.el.css('visibility', 'hidden')
 
   startApp: =>
-    @loadData() unless @dataLoaded
     unless @appStarted
-      @append @mainStack, @helpUI
+      @loadData()
       @navigate('/')
       @appStarted = true
 
   endApp: =>
     if @appStarted
-      @mainStack.el.remove()
-      @helpUI.el.remove()
+      @unloadData()
+      @mainStack.filterBox.reset()
       @appStarted = false
 
   loadData: =>
@@ -110,16 +111,13 @@ class App extends Spine.Controller
   setupOnlineOffline: ->   
     if not navigator.onLine
       @mainNav.offline.show()
-      Spine.trigger 'app:offline'
       
     $(window).on 'offline', =>
       @mainNav.offline.show()
-      Spine.trigger 'app:offline'
     
     $(window).on 'online', =>
       @mainNav.offline.hide()
-      @getSessionInfo()
-      Spine.trigger 'app:online'
+      @delay @getSessionInfo, 5000
 
   doOtherStuff: ->
     # Use the fastclick module for touch devices.

+ 2 - 2
admin/controllers/main-nav.coffee

@@ -88,8 +88,8 @@ class MainNav extends Spine.Controller
         .addClass('show')
     hide = ->
       @userGreeting.removeClass('show')
-    @delay show, 1*1000
-    @delay hide, 10*1000
+    @delay show, 200
+    @delay hide, 5000
 
 
 module.exports = MainNav

+ 6 - 1
admin/controllers/scenes.coffee

@@ -192,6 +192,10 @@ class SceneForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -216,7 +220,7 @@ class SceneForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -237,6 +241,7 @@ class SceneForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 6 - 1
admin/controllers/sites.coffee

@@ -67,6 +67,10 @@ class SiteForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -95,7 +99,7 @@ class SiteForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -116,6 +120,7 @@ class SiteForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 6 - 1
admin/controllers/sponsors.coffee

@@ -80,6 +80,10 @@ class SponsorForm extends Spine.Controller
 
   save: (e) ->
     e.preventDefault()
+    if not navigator.onLine
+      alert "Can not save. You are OFFLINE."
+      return
+      
     if @editing
       @item.fromForm(@form)
     else
@@ -100,7 +104,7 @@ class SponsorForm extends Spine.Controller
   
   destroy: (e) ->
     e.preventDefault()
-    if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
+    if @item and confirm "Are you sure you want to delete this item?"
       @item.destroy()
       @back()
 
@@ -121,6 +125,7 @@ class SponsorForm extends Spine.Controller
 
   preventSubmit: (e) ->
     e.preventDefault()
+    return false
     
   deactivate: ->
     @el.scrollTop(0)

+ 5 - 0
admin/static/css/theme.styl

@@ -219,6 +219,11 @@ span.label
       margin: 30px
       padding: 0
 
+    .offline
+      top: 32px
+      right: 0
+      left: auto
+
     > form.login
       display: inline-block
 

+ 3 - 2
site/lib/utils.coffee

@@ -2,8 +2,9 @@ moment    = require('lib/moment')
 
 exports.isDev = (req) ->
   # We are on DEV when Host has string like
-  # '/dev.' or '/staging.' or '.local' or 'localhost'
-  !!(req.headers.Host.match(/(\/dev\.|\/staging\.|\.local$|localhost)/))
+  # '/dev.' or '/staging.' or '.local' or '.dev' or 'localhost'
+  found = req.headers.Host.search(/(\/dev\.|\/staging\.|\.local$|\.dev$|localhost)/) > -1
+  return found
 
 exports.prettyDate = (date) ->
   moment.utc(date).local().format('MMM Do YYYY')