Browse Source

Fixed signature insert.

Martin Edenhofer 8 years ago
parent
commit
977c05204c

+ 9 - 11
app/assets/javascripts/app/controllers/agent_ticket_create.coffee

@@ -232,23 +232,21 @@ class App.TicketCreate extends App.Controller
         if signature isnt undefined &&  signature.body && type is 'email-out'
           signatureFinished = App.Utils.replaceTags(signature.body, { user: App.Session.get() })
 
-          # get current body
-          body = @$('[data-name="body"]').html() || ''
-          if App.Utils.signatureCheck(body, signatureFinished)
+          body = @$('[data-name=body]')
+          if App.Utils.signatureCheck(body.html() || '', signatureFinished)
 
-            # if signature has changed, replace it
+            # if signature has changed, in case remove old signature
             signature_id = @$('[data-signature=true]').data('signature-id')
             if signature_id && signature_id.toString() isnt signature.id.toString()
 
-              # remove old signature
               @$('[data-signature="true"]').remove()
-              body = @$('[data-name="body"]').html() || ''
 
-            if !App.Utils.lastLineEmpty(body)
-              body = body + '<br>'
-            body = body + "<div><div data-signature=\"true\" data-signature-id=\"#{signature.id}\">#{signatureFinished}</div></div>"
-
-            @$('[data-name="body"]').html(body)
+            if !App.Utils.htmlLastLineEmpty(body)
+              body.append('<br><br>')
+            signature = $("<div data-signature=\"true\" data-signature-id=\"#{signature.id}\">#{signatureFinished}</div>")
+            App.Utils.htmlStrip(signature)
+            body.append(signature)
+            @$('[data-name=body]').replaceWith(body)
 
         # remove old signature
         else

+ 27 - 7
app/assets/javascripts/app/lib/app_post/utils.coffee

@@ -109,6 +109,33 @@ class App.Utils
       .replace(/"/g, '&quot;')
       .replace(/'/g, '&#39;')
 
+  # App.Utils.htmlStrip(element)
+  @htmlStrip: (element) ->
+    loop
+      el = element.get(0)
+      break if !el
+      child = el.firstChild
+      break if !child
+      break if child.nodeType isnt 1 || child.tagName isnt 'BR'
+      child.remove()
+
+    loop
+      el = element.get(0)
+      break if !el
+      child = el.lastChild
+      break if !child
+      break if child.nodeType isnt 1 || child.tagName isnt 'BR'
+      child.remove()
+
+  # true|false = App.Utils.htmlLastLineEmpty(element)
+  @htmlLastLineEmpty: (element) ->
+    el = element.get(0)
+    return false if !el
+    child = el.lastChild
+    return false if !child
+    return false if child.nodeType isnt 1 || child.tagName isnt 'BR'
+    true
+
   # textWithoutTags = App.Utils.htmlRemoveTags(html)
   @htmlRemoveTags: (html) ->
     html = @_checkTypeOf(html)
@@ -503,13 +530,6 @@ class App.Utils
       value
     )
 
-  # true|false = App.Utils.lastLineEmpty(message)
-  @lastLineEmpty: (message) ->
-    messageCleanup = message.replace(/>\s+</g, '><').replace(/(\n|\r|\t)/g, '').trim()
-    return true if messageCleanup.match(/<(br|\s+?|\/)>$/im)
-    return true if messageCleanup.match(/<div(|\s.+?)><\/div>$/im)
-    false
-
   # string = App.Utils.removeEmptyLines(stringWithEmptyLines)
   @removeEmptyLines: (string) ->
     string.replace(/^\s*[\r\n]/gm, '')