diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index 62262e123..03b5ff425 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -3,10 +3,10 @@ class App.TicketCreate extends App.Controller '.tabsSidebar' : 'sidebar' events: - 'click .type-tabs .tab': 'changeFormType' - 'submit form': 'submit' - 'click .submit': 'submit' - 'click .cancel': 'cancel' + 'click .type-tabs .tab': 'changeFormType' + 'submit form': 'submit' + 'click .submit': 'submit' + 'click .cancel': 'cancel' constructor: (params) -> super @@ -87,6 +87,9 @@ class App.TicketCreate extends App.Controller # update form @el.find('[name="formSenderType"]').val(type) + # force changing signature + @el.find('[name="group_id"]').trigger('change') + meta: => text = '' if @articleAttributes @@ -167,10 +170,10 @@ class App.TicketCreate extends App.Controller a = App.TicketArticle.find( params.article_id ) # reset owner - t.owner_id = 0 - t.customer_id_autocompletion = a.from - t.subject = a.subject || t.title - t.body = a.body + t.owner_id = 0 + t.customer_id_completion = a.from + t.subject = a.subject || t.title + t.body = a.body # render page @render( options: t ) @@ -191,10 +194,10 @@ class App.TicketCreate extends App.Controller @form_id = App.ControllerForm.formId() @html App.view('agent_ticket_create')( - head: 'New Ticket' - agent: @isRole('Agent') - admin: @isRole('Admin') - form_id: @form_id + head: 'New Ticket' + agent: @isRole('Agent') + admin: @isRole('Admin') + form_id: @form_id ) formChanges = (params, attribute, attributes, classname, form, ui) => @@ -220,6 +223,35 @@ class App.TicketCreate extends App.Controller # replace new option list form.find('[name="' + fieldNameToChange + '"]').closest('.form-group').replaceWith( newElement ) + signatureChanges = (params, attribute, attributes, classname, form, ui) => + if attribute && attribute.name is 'group_id' + signature = undefined + if params['group_id'] + group = App.Group.find( params['group_id'] ) + if group && group.signature_id + signature = App.Signature.find( group.signature_id ) + + # check if signature need to be added + type = @$('[name="formSenderType"]').val() + + if signature isnt undefined && signature.body && type is 'email-out' + signatureFinished = App.Utils.text2html( + App.Utils.replaceTags( signature.body, { user: App.Session.get() } ) + ) + + # get current body + body = @$('[data-name="body"]').html() || '' + if App.Utils.signatureCheck( body, signatureFinished ) + if !App.Utils.lastLineEmpty(body) + body = body + '
' + body = body + "
#{signatureFinished}
" + + @$('[data-name="body"]').html(body) + + # remove old signature + else + @$('[data-name="body"]').find("[data-signature=true]").remove() + new App.ControllerForm( el: @el.find('.ticket-form-top') form_id: @form_id @@ -228,7 +260,8 @@ class App.TicketCreate extends App.Controller events: 'change [name=customer_id]': @localUserInfo handlers: [ - formChanges + formChanges, + signatureChanges, ] filter: @form_meta.filter autofocus: true @@ -236,21 +269,22 @@ class App.TicketCreate extends App.Controller ) new App.ControllerForm( - el: @el.find('.article-form-top') - form_id: @form_id - model: App.TicketArticle - screen: 'create_top' - params: params + el: @el.find('.article-form-top') + form_id: @form_id + model: App.TicketArticle + screen: 'create_top' + params: params ) new App.ControllerForm( - el: @el.find('.ticket-form-middle') - form_id: @form_id - model: App.Ticket - screen: 'create_middle' + el: @el.find('.ticket-form-middle') + form_id: @form_id + model: App.Ticket + screen: 'create_middle' events: 'change [name=customer_id]': @localUserInfo handlers: [ - formChanges + formChanges, + signatureChanges, ] filter: @form_meta.filter params: params @@ -264,7 +298,8 @@ class App.TicketCreate extends App.Controller events: 'change [name=customer_id]': @localUserInfo handlers: [ - formChanges + formChanges, + signatureChanges, ] filter: @form_meta.filter params: params @@ -330,7 +365,7 @@ class App.TicketCreate extends App.Controller if sender.name is 'Customer' params['article'] = { to: (group && group.name) || '' - from: params.customer_id_autocompletion + from: params.customer_id_completion cc: params.cc subject: params.subject body: params.body @@ -342,7 +377,7 @@ class App.TicketCreate extends App.Controller else params['article'] = { from: (group && group.name) || '' - to: params.customer_id_autocompletion + to: params.customer_id_completion cc: params.cc subject: params.subject body: params.body diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index 85f897f11..8fd4bb0f4 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -18,10 +18,9 @@ class App.TicketZoom extends App.Controller @navupdate '#' - @form_meta = undefined - @ticket_id = params.ticket_id - @article_id = params.article_id - @signature = undefined + @form_meta = undefined + @ticket_id = params.ticket_id + @article_id = params.article_id @key = 'ticket::' + @ticket_id cache = App.Store.get( @key ) @@ -137,9 +136,6 @@ class App.TicketZoom extends App.Controller # get edit form attributes @form_meta = data.form_meta - # get signature - @signature = data.signature - # load assets App.Collection.loadAssets( data.assets ) @@ -860,14 +856,15 @@ class Edit extends App.Controller if data.ticket.id is @ticket.id #@setArticleType(data.type.name) - # preselect article type - @setArticleType( 'email' ) @open_textarea(null, true) for key, value of data.article if key is 'body' @$('[data-name="' + key + '"]').html(value) else @$('[name="' + key + '"]').val(value) + + # preselect article type + @setArticleType( 'email' ) ) isIE10: -> @@ -905,7 +902,7 @@ class Edit extends App.Controller @$('[data-name="body"]').ce({ mode: 'richtext' multiline: true - maxlength: 2500 + maxlength: 5000 }) html5Upload.initialize( @@ -1075,10 +1072,29 @@ class Edit extends App.Controller # show/hide attributes for articleType in @articleTypes if articleType.name is type - @$(".form-group").addClass('hide') + @$('.form-group').addClass('hide') for name in articleType.attributes @$("[name=#{name}]").closest('.form-group').removeClass('hide') + # check if signature need to be added + body = @$('[data-name="body"]').html() || '' + signature = undefined + if @ticket.group.signature_id + signature = App.Signature.find( @ticket.group.signature_id ) + if signature && signature.body && @type is 'email' + signatureFinished = App.Utils.text2html( + App.Utils.replaceTags( signature.body, { user: App.Session.get(), ticket: @ticket } ) + ) + if App.Utils.signatureCheck( body, signatureFinished ) + if !App.Utils.lastLineEmpty(body) + body = body + '
' + body = body + "
#{signatureFinished}
" + @$('[data-name="body"]').html(body) + + # remove old signature + else + @$('[data-name="body"]').find("[data-signature=true]").remove() + detect_empty_textarea: => if !@textarea.text().trim() @add_textarea_catcher() @@ -1488,12 +1504,6 @@ class ArticleView extends App.Controller # get current body body = @ui.el.find('[data-name="body"]').html() || '' - # check if signature need to be added - if @ui.signature && @ui.signature.body && type.name is 'email' - signature = App.Utils.text2html( @ui.signature.body ) - if App.Utils.signatureCheck( body, signature ) - body = body + signature - # check if quote need to be added selectedText = App.ClipBoard.getSelected() if selectedText diff --git a/app/assets/javascripts/app/lib/app_post/utils.js.coffee b/app/assets/javascripts/app/lib/app_post/utils.js.coffee index 5f576a74f..6690bf4e7 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.js.coffee @@ -166,3 +166,27 @@ class App.Utils else true + # textReplaced = App.Utils.replaceTags( template, { user: { firstname: 'Bob', lastname: 'Smith' } } ) + @replaceTags: (template, objects) -> + template = template.replace( /#\{\s{0,2}(.+?)\s{0,2}\}/g, ( index, key ) -> + levels = key.split(/\./) + dataRef = objects + for level in levels + if dataRef[level] + dataRef = dataRef[level] + if typeof dataRef is 'function' + value = dataRef() + else if typeof dataRef is 'string' + value = dataRef + else + value = '' + #console.log( "tag replacement #{key}, #{value} env: ", objects) + value + ) + + # true|false = App.Utils.lastLineEmpty( message ) + @lastLineEmpty: (message) -> + messageCleanup = message.replace(/>\s+<').replace(/(\n|\r|\t)/g, '').trim() + return true if messageCleanup.match(/<(br|\s+?|\/)>$/im) + return true if messageCleanup.match(/<\/div>$/im) + false diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 696181dbd..b8fc5b27b 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -228,22 +228,6 @@ class TicketsController < ApplicationController ticket = Ticket.find( params[:id] ) return if !ticket_permission( ticket ) - # get signature - signature = {} - if ticket.group.signature - signature = ticket.group.signature.attributes - - # replace tags - signature['body'] = NotificationFactory.build( - :locale => current_user.preferences[:locale], - :string => signature['body'], - :objects => { - :ticket => ticket, - :user => current_user, - } - ) - end - # get attributes to update attributes_to_change = Ticket::ScreenOptions.attributes_to_change( :user => current_user, :ticket => ticket ) @@ -292,7 +276,6 @@ class TicketsController < ApplicationController render :json => { :ticket_id => ticket.id, :ticket_article_ids => article_ids, - :signature => signature, :assets => assets, :links => link_list, :tags => tags, diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index 392e7febb..fff291fe2 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -439,4 +439,92 @@ test( "check signature", function() { }); +// replace tags +test( "check replace tags", function() { + + var message = "
#{user.firstname} #{user.lastname}
" + var result = '
Bob Smith
' + var data = { + user: { + firstname: 'Bob', + lastname: 'Smith', + }, + } + var verify = App.Utils.replaceTags( message, data ) + equal( verify, result ) + + message = "
#{user.firstname} #{user.lastname}
" + result = '
Bob Smith
' + data = { + user: { + firstname: function() { return 'Bob' }, + lastname: function() { return 'Smith' }, + }, + } + verify = App.Utils.replaceTags( message, data ) + equal( verify, result ) + + message = "
#{user.firstname} #{user.lastname}
" + result = '
Bob
' + data = { + user: { + firstname: 'Bob', + }, + } + verify = App.Utils.replaceTags( message, data ) + equal( verify, result ) + +}); + +// check if last line is a empty line +test( "check if last line is a empty line", function() { + + var message = "123" + var result = false + var verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
123
" + result = false + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "

123

" + result = false + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
" + result = true + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
" + result = true + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
" + result = true + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
\n \n\t" + result = true + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
\n \n\t" + result = true + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + message = "
\n
\n \n\t" + result = true + verify = App.Utils.lastLineEmpty( message ) + equal( verify, result, message ) + + +}); + } \ No newline at end of file