Browse Source

Introduced App.Utils class.

Martin Edenhofer 10 years ago
parent
commit
4f98f39211

+ 3 - 3
app/assets/javascripts/app/controllers/_application_controller.js.coffee

@@ -229,7 +229,7 @@ class App.Controller extends Spine.Controller
       title: ->
         ticket_id = $(@).data('id')
         ticket    = App.Ticket.fullLocal( ticket_id )
-        HTMLEscape( ticket.title )
+        App.Utils.htmlEscape( ticket.title )
       content: ->
         ticket_id        = $(@).data('id')
         ticket           = App.Ticket.fullLocal( ticket_id )
@@ -264,7 +264,7 @@ class App.Controller extends Spine.Controller
       title: ->
         user_id = $(@).data('id')
         user    = App.User.fullLocal( user_id )
-        HTMLEscape( user.displayName() )
+        App.Utils.htmlEscape( user.displayName() )
       content: ->
         user_id = $(@).data('id')
         user    = App.User.fullLocal( user_id )
@@ -311,7 +311,7 @@ class App.Controller extends Spine.Controller
       title: ->
         organization_id = $(@).data('id')
         organization    = App.Organization.fullLocal( organization_id )
-        HTMLEscape( organization.name )
+        App.Utils.htmlEscape( organization.name )
       content: ->
         organization_id = $(@).data('id')
         organization    = App.Organization.fullLocal( organization_id )

+ 2 - 2
app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee

@@ -442,12 +442,12 @@ class App.GenericHistory extends App.ControllerModal
         if item.value_from
           if item.value_to
             content += " #{ @T( 'from' ) }"
-          content += " '#{ HTMLEscape(item.value_from) }'"
+          content += " '#{ App.Utils.htmlEscape(item.value_from) }'"
 
         if item.value_to
           if item.value_from
             content += " #{ @T( 'to' ) }"
-          content += " '#{ HTMLEscape(item.value_to) }'"
+          content += " '#{ App.Utils.htmlEscape(item.value_to) }'"
 
       newItem.records.push content
 

+ 25 - 23
app/assets/javascripts/app/controllers/ticket_zoom.js.coffee

@@ -145,7 +145,6 @@ class App.TicketZoom extends App.Controller
     # get data
     @ticket = App.Ticket.fullLocal( @ticket_id )
     @ticket.article = undefined
-    console.log('KLKL',@ticket)
 
     # render page
     @render(force)
@@ -849,7 +848,7 @@ class Edit extends App.Controller
           @open_textarea(null, true)
           for key, value of data.article
             if key is 'body'
-              @$('[data-name="' + key + '"]').text(value)
+              @$('[data-name="' + key + '"]').html(value)
             else
               @$('[name="' + key + '"]').val(value)
     )
@@ -1384,17 +1383,20 @@ class ArticleView extends App.Controller
     false
 
   checkIfSignatureIsNeeded: (type) =>
-
+    console.log('checkIfSignatureIsNeeded', type, @ui.signature)
     # add signature
     if @ui.signature && @ui.signature.body && type.name is 'email'
-      body   = @ui.el.find('[name="body"]').val() || ''
-      regexp = new RegExp( escapeRegExp( @ui.signature.body ) , 'i')
-      if !body.match(regexp)
-        body = body + "\n" + @ui.signature.body
-        @ui.el.find('[name="body"]').val( body )
-
-        # update textarea size
-        @ui.el.find('[name="body"]').trigger('change')
+      body = @ui.el.find('[data-name="body"]').html() || ''
+
+      # convert to html
+      signature = @ui.signature.body
+      #signature = signature.replace(//g, " ")
+      signature = '<p>' + signature.replace(/\n/g, '</p><p>') + '</p>'
+      regexp = new RegExp( escapeRegExp( signature ) , 'im')
+      #console.log('aaa', body, regexp)
+      if !body || !body.match(regexp)
+        body = body + signature
+        @ui.el.find('[data-name="body"]').html( body )
 
   replyAll: (e) =>
     @reply(e, true)
@@ -1483,13 +1485,18 @@ class ArticleView extends App.Controller
 
     # add quoted text if needed
     selectedText = App.ClipBoard.getSelected()
-    console.log('selectedText', selectedText)
+
     if selectedText
-      body = @ui.el.find('[data-name="body"]').text() || ''
+      body = @ui.el.find('[data-name="body"]').html() || ''
+
+      # quote text
       selectedText = selectedText.replace /^(.*)$/mg, (match) =>
         '> ' + match
-      body = selectedText + "\n" + body
-      articleNew.body = body
+
+      # convert to html
+      selectedText = '<p>' + selectedText.replace(/\n/g, "</p><p>") + '</p>'
+
+      articleNew.body = selectedText + body
 
     App.Event.trigger('ui::ticket::setArticleType', { ticket: @ticket, type: type, article: articleNew } )
 
@@ -1514,12 +1521,7 @@ class Article extends App.Controller
       return
 
     # build html body
-    # cleanup body
-#    @article['html'] = @article.body.trim()
-    @article['html'] = $.trim( @article.body )
-    @article['html'].replace( /\n\r/g, "\n" )
-    @article['html'].replace( /\r/g, "\n" )
-    @article['html'].replace( /\n\n/g, "\n" )
+    @article['html'] = App.Utils.textCleanup( @article.body )
 
     # if body has more then x lines / else search for signature
     preview       = 10
@@ -1532,7 +1534,7 @@ class Article extends App.Controller
       else
         article_lines.splice( preview - 1, 0, '-----SEEMORE-----' )
       @article['html'] = article_lines.join("\n")
-    @article['html'] = window.linkify( @article['html'] )
+    @article['html'] = App.Utils.linkify( @article['html'] )
     notify = '<a href="#" class="show_toogle">' + App.i18n.translateContent('See more') + '</a>'
 
     # preview mode
@@ -1553,7 +1555,7 @@ class Article extends App.Controller
       if @article_changed
         @article['html'] = @article['html'] + '</div>'
 
-    @article['html'] = @article['html'].replace( /\n/g, '<br>' )
+    @article['html'] = App.Utils.text2html( @article.body )
 
   actionRow: ->
     if @isRole('Customer')

+ 3 - 3
app/assets/javascripts/app/index.js.coffee

@@ -92,7 +92,7 @@ class App extends Spine.Controller
       # define linkify helper
       params.L = ( item ) ->
         if item && typeof item is 'string'
-          return window.linkify( item )
+          return App.Utils.linkify( item )
         item
 
       # define config helper
@@ -118,9 +118,9 @@ class App extends Spine.Controller
           if result
             result = result + ', '
           if item.name
-            result = result + HTMLEscape(item.name) + ' '
+            result = result + App.Utils.htmlEscape(item.name) + ' '
           if item.address
-            result = result + " <span class=\"text-muted\">&lt;#{HTMLEscape(item.address)}&gt</span>"
+            result = result + " <span class=\"text-muted\">&lt;#{App.Utils.htmlEscape(item.address)}&gt</span>"
 
         result
 

+ 4 - 4
app/assets/javascripts/app/lib/app_post/i18n.js.coffee

@@ -130,13 +130,13 @@ class _i18nSingleton extends Spine.Module
     )
 
   translateInline: ( string, args... ) =>
-    HTMLEscape( @translate( string, args... ) )
+    App.Utils.htmlEscape( @translate( string, args... ) )
 
   translateContent: ( string, args... ) =>
-    translated = HTMLEscape( @translate( string, args... ) )
-#    replace = '<span class="translation" contenteditable="true" data-text="' + HTMLEscape(string) + '">' + translated + '<span class="icon-edit"></span>'
+    translated = App.Utils.htmlEscape( @translate( string, args... ) )
+#    replace = '<span class="translation" contenteditable="true" data-text="' + App.Utils.htmlEscape(string) + '">' + translated + '<span class="icon-edit"></span>'
     if App.Config.get( 'Translation' )
-      replace = '<span class="translation" contenteditable="true" data-text="' + HTMLEscape(string) + '">' + translated + ''
+      replace = '<span class="translation" contenteditable="true" data-text="' + App.Utils.htmlEscape(string) + '">' + translated + ''
   #    if !@_translated
   #       replace += '<span class="missing">XX</span>'
       replace += '</span>'

+ 32 - 0
app/assets/javascripts/app/lib/app_post/utils.js.coffee

@@ -0,0 +1,32 @@
+class App.Utils
+
+  # textCleand = App.Utils.textCleanup( rawText )
+
+  @textCleanup: ( ascii ) ->
+    $.trim( ascii )
+      .replace(/(\r\n|\n\r)/g, "\n")  # cleanup
+      .replace(/\r/g, "\n")           # cleanup
+      .replace(/\s+$/gm, "\n")        # remove tailing spaces
+      .replace(/\n{2,9}/gm, "\n\n")   # remove multible empty lines
+
+  # htmlEscapedAndLinkified = App.Utils.text2html( rawText )
+
+  @text2html: ( ascii ) ->
+    ascii = @textCleanup(ascii)
+    #ascii = @htmlEscape(ascii)
+    ascii = @linkify(ascii)
+    ascii.replace( /\n/g, '<br>' )
+
+  # htmlEscaped = App.Utils.htmlEscape( rawText )
+
+  @htmlEscape: ( ascii ) ->
+    ascii.replace(/&/g, '&amp;')
+      .replace(/</g, '&lt;')
+      .replace(/>/g, '&gt;')
+      .replace(/"/g, '&quot;')
+      .replace(/'/g, '&#39;')
+
+  # htmlEscapedAndLinkified = App.Utils.linkify( rawText )
+
+  @linkify: (ascii) ->
+    window.linkify( ascii )

+ 0 - 9
app/assets/javascripts/application.js

@@ -76,15 +76,6 @@ function clone(object) {
   return JSON.parse(JSON.stringify(object));
 }
 
-function HTMLEscape(string) {
-  return ( '' + string )
-    .replace(/&/g, '&amp;')
-    .replace(/</g, '&lt;')
-    .replace(/>/g, '&gt;')
-    .replace(/"/g, '&quot;')
-    .replace(/'/g, '&#39;')
-}
-
 jQuery.event.special.remove = {
   remove: function(e) {
     if (e.handler) e.handler();

+ 7 - 0
app/controllers/tests_controller.rb

@@ -37,6 +37,13 @@ class TestsController < ApplicationController
     end
   end
 
+  # GET /tests/html_utils
+  def html_utils
+    respond_to do |format|
+      format.html # index.html.erb
+    end
+  end
+
   # GET /test/wait
   def wait
     sleep params[:sec].to_i

+ 15 - 0
app/views/tests/html_utils.html.erb

@@ -0,0 +1,15 @@
+
+<link rel="stylesheet" href="/assets/tests/qunit-1.10.0.css">
+<script src="/assets/tests/qunit-1.10.0.js"></script>
+<script src="/assets/tests/html-utils.js"></script>
+
+<style type="text/css">
+body {
+  padding-top: 0px;
+}
+</style>
+
+<script type="text/javascript">
+</script>
+
+<div id="qunit"></div>

+ 1 - 0
config/routes/test.rb

@@ -6,6 +6,7 @@ Zammad::Application.routes.draw do
   match '/tests-form',          :to => 'tests#form',          :via => :get
   match '/tests-form-extended', :to => 'tests#form_extended', :via => :get
   match '/tests-table',         :to => 'tests#table',         :via => :get
+  match '/tests-html-utils',    :to => 'tests#html_utils',    :via => :get
   match '/tests/wait/:sec',     :to => 'tests#wait',          :via => :get
 
 end

Some files were not shown because too many files changed in this diff