/
ticket_zoom.coffee
1040 lines (847 loc) · 30.2 KB
/
ticket_zoom.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class App.TicketZoom extends App.Controller
@include App.TicketNavigable
elements:
'.main': 'main'
'.ticketZoom': 'ticketZoom'
'.scrollPageHeader': 'scrollPageHeader'
events:
'click .js-submit': 'submit'
'click .js-bookmark': 'bookmark'
'click .js-reset': 'reset'
'click .main': 'muteTask'
constructor: (params) ->
super
# check authentication
@authenticateCheckRedirect()
@formMeta = undefined
@ticket_id = params.ticket_id
@article_id = params.article_id
@sidebarState = {}
# if we are in init task startup, ignore overview_id
if !params.init
@overview_id = params.overview_id
else
@overview_id = undefined
@key = "ticket::#{@ticket_id}"
cache = App.SessionStorage.get(@key)
if cache
@load(cache)
# check if ticket has been updated every 30 min
update = =>
if !@initFetched
@fetch()
return
@fetch(true)
@interval(update, 1800000, 'pull_check')
# fetch new data if triggered
@bind('Ticket:update Ticket:touch', (data) =>
# check if current ticket has changed
return if data.id.toString() isnt @ticket_id.toString()
# check if we already have the request queued
#@log 'notice', 'TRY', @ticket_id, new Date(data.updated_at), new Date(@ticketUpdatedAtLastCall)
@fetchMayBe(data)
)
# after a new websocket connection, check if ticket has changed
@bind('spool:sent', =>
if @initSpoolSent
@fetch(true)
return
@initSpoolSent = true
)
# listen to rerender sidebars
@bind('ui::ticket::sidebarRerender', (data) =>
return if data.taskKey isnt @taskKey
return if !@sidebarWidget
@sidebarWidget.render(@formCurrent())
)
fetchMayBe: (data) =>
if @ticketUpdatedAtLastCall
if new Date(data.updated_at).getTime() is new Date(@ticketUpdatedAtLastCall).getTime()
#console.log('debug no fetch, current ticket already there or requested')
return
if new Date(data.updated_at).getTime() < new Date(@ticketUpdatedAtLastCall).getTime()
#console.log('debug no fetch, current ticket already newer or requested')
return
@ticketUpdatedAtLastCall = data.updated_at
fetchDelayed = =>
@fetch()
@delay(fetchDelayed, 1000, "ticket-zoom-#{@ticket_id}")
fetch: (ignoreSame = false) =>
return if !@Session.get()
queue = false
if !@initFetched
queue = true
# get data
@ajax(
id: "ticket_zoom_#{@ticket_id}"
type: 'GET'
url: "#{@apiPath}/tickets/#{@ticket_id}?all=true"
processData: true
queue: queue
success: (data, status, xhr) =>
@load(data, ignoreSame)
App.SessionStorage.set(@key, data)
error: (xhr) =>
statusText = xhr.statusText
status = xhr.status
detail = xhr.responseText
# ignore if request is aborted
return if statusText is 'abort'
@renderDone = false
# if ticket is already loaded, ignore status "0" - network issues e. g. temp. not connection
if @ticketUpdatedAtLastCall && status is 0
console.log('network issues e. g. temp. no connection', status, statusText, detail)
return
# show error message
if status is 401 || statusText is 'Unauthorized'
@taskHead = '» ' + App.i18n.translateInline('Unauthorized') + ' «'
@taskIconClass = 'diagonal-cross'
@renderScreenUnauthorized(objectName: 'Ticket')
else if status is 404 || statusText is 'Not Found'
@taskHead = '» ' + App.i18n.translateInline('Not Found') + ' «'
@taskIconClass = 'diagonal-cross'
@renderScreenNotFound(objectName: 'Ticket')
else
@taskHead = '» ' + App.i18n.translateInline('Error') + ' «'
@taskIconClass = 'diagonal-cross'
if !detail
detail = 'General communication error, maybe internet is not available!'
@renderScreenError(
status: status
detail: detail
objectName: 'Ticket'
)
)
load: (data, ignoreSame = false, local = false) =>
# check if ticket has changed
newTicketRaw = data.assets.Ticket[@ticket_id]
#console.log(newTicketRaw.updated_at)
#console.log(@ticketUpdatedAtLastCall)
if @ticketUpdatedAtLastCall
# ignore if record is already shown
if ignoreSame && new Date(newTicketRaw.updated_at).getTime() is new Date(@ticketUpdatedAtLastCall).getTime()
#console.log('debug no fetched, current ticket already there or requested')
return
# do not render if newer ticket is already requested
if new Date(newTicketRaw.updated_at).getTime() < new Date(@ticketUpdatedAtLastCall).getTime()
#console.log('fetched no fetch, current ticket already newer')
return
# remember current record if newer as requested record
if new Date(newTicketRaw.updated_at).getTime() > new Date(@ticketUpdatedAtLastCall).getTime()
@ticketUpdatedAtLastCall = newTicketRaw.updated_at
else
@ticketUpdatedAtLastCall = newTicketRaw.updated_at
# notify if ticket changed not by my self
if @initFetched
if newTicketRaw.updated_by_id isnt @Session.get('id')
App.TaskManager.notify(@taskKey)
@initFetched = true
if !@doNotLog
@doNotLog = 1
@recentView('Ticket', @ticket_id)
# remember article ids
@ticket_article_ids = data.ticket_article_ids
# remember link
@links = data.links
# remember tags
@tags = data.tags
# get edit form attributes
@formMeta = data.form_meta
# load assets
App.Collection.loadAssets(data.assets, targetModel: 'Ticket')
# get data
@ticket = App.Ticket.fullLocal(@ticket_id)
@ticket.article = undefined
# render page
@render(local)
meta: =>
# default attributes
meta =
url: @url()
id: @ticket_id
# set icon and title if defined
if @taskIconClass
meta.iconClass = @taskIconClass
if @taskHead
meta.head = @taskHead
# set icon and title based on ticket
if @ticket_id && App.Ticket.exists(@ticket_id)
ticket = App.Ticket.findNative(@ticket_id)
meta.head = ticket.title
meta.title = "##{ticket.number} - #{ticket.title}"
meta.class = "task-state-#{ ticket.getState() }"
meta.type = 'task'
meta.iconTitle = ticket.iconTitle()
meta.iconClass = ticket.iconClass()
meta
url: =>
"#ticket/zoom/#{@ticket_id}"
show: (params) =>
@navupdate(url: '#', type: 'menu')
# set all notifications to seen
App.OnlineNotification.seen('Ticket', @ticket_id)
# if controller is executed twice, go to latest article (e. g. click on notification)
if @activeState
if @ticket_article_ids
@shown = false
@activeState = true
@pagePosition(params)
@positionPageHeaderStart()
@autosaveStart()
@shortcutNavigationStart()
return if !@attributeBar
@attributeBar.start()
# scroll to article if given
scrollToPosition: (position, delay, article_id) =>
scrollToDelay = =>
if position is 'article'
@scrollToArticle(article_id)
@positionPageHeaderUpdate()
return
@scrollToBottom()
@positionPageHeaderUpdate()
@delay(scrollToDelay, delay, 'scrollToPosition')
pagePosition: (params = {}) =>
# remember for later
return if params.type is 'init' && !@shown
if params.article_id
article_id = params.article_id
params.article_id = undefined
else if @pagePositionData
article_id = @pagePositionData
@pagePositionData = undefined
# trigger shown to article
if !@shown
@shown = true
App.Event.trigger('ui::ticket::shown', { ticket_id: @ticket_id })
@scrollToPosition('bottom', 50, article_id)
return
# scroll to article if given
if article_id && article_id isnt @last_article_id
@last_article_id = article_id
@scrollToPosition('article', 300, article_id)
return
# scroll to end if new article has been added
if !@last_ticket_article_ids || !_.isEqual(_.sortBy(@last_ticket_article_ids), _.sortBy(@ticket_article_ids))
@last_ticket_article_ids = @ticket_article_ids
@scrollToPosition('bottom', 100, article_id)
return
setPosition: (position) =>
@$('.main').scrollTop(position)
currentPosition: =>
element = @$('.main .ticketZoom')
offset = element.offset()
if offset
position = offset.top
Math.abs(position)
hide: =>
@activeState = false
$('body > .modal').modal('hide')
@positionPageHeaderStop()
@autosaveStop()
@shortcutNavigationstop()
return if !@attributeBar
@attributeBar.stop()
changed: =>
return false if !@ticket
currentParams = @formCurrent()
currentStore = @currentStore()
modelDiff = @formDiff(currentParams, currentStore)
return false if !modelDiff || _.isEmpty(modelDiff)
return false if _.isEmpty(modelDiff.ticket) && _.isEmpty(modelDiff.article)
return true
release: =>
@autosaveStop()
@positionPageHeaderStop()
muteTask: =>
App.TaskManager.mute(@taskKey)
shortcutNavigationStart: =>
@articlePager =
article_id: undefined
modifier = 'alt+ctrl+left'
$(document).bind("keydown.ticket_zoom#{@ticket_id}", modifier, (e) =>
@articleNavigate('up')
)
modifier = 'alt+ctrl+right'
$(document).bind("keydown.ticket_zoom#{@ticket_id}", modifier, (e) =>
@articleNavigate('down')
)
shortcutNavigationstop: =>
$(document).unbind("keydown.ticket_zoom#{@ticket_id}")
articleNavigate: (direction) =>
articleStates = []
@$('.ticket-article .ticket-article-item').each( (_index, element) ->
$element = $(element)
article_id = $element.data('id')
visible = $element.visible(true)
articleStates.push {
article_id: article_id
visible: visible
}
)
# navigate to article
if direction is 'up'
articleStates = articleStates.reverse()
jumpTo = undefined
for articleState in articleStates
if jumpTo
@scrollToArticle(articleState.article_id)
@articlePager.article_id = articleState.article_id
return
if @articlePager.article_id
if @articlePager.article_id is articleState.article_id
jumpTo = articleState.article_id
else
if articleState.visible
jumpTo = articleState.article_id
positionPageHeaderStart: =>
# init header update needed for safari, scroll event is fired
@positionPageHeaderUpdate()
# scroll is also fired on window resize, if element scroll is changed
@main.bind(
'scroll'
@positionPageHeaderUpdate
)
positionPageHeaderStop: =>
@main.unbind('scroll', @positionPageHeaderUpdate)
@scrollHeaderPos: undefined
positionPageHeaderUpdate: =>
headerHeight = @scrollPageHeader.outerHeight()
mainScrollHeigth = @main.prop('scrollHeight')
mainHeigth = @main.height()
scroll = @main.scrollTop()
# if page header is not possible to use - mainScrollHeigth to low - hide page header
if not mainScrollHeigth > mainHeigth + headerHeight
@scrollPageHeader.css('transform', "translateY(#{-headerHeight}px)")
return
if scroll > headerHeight
scroll = headerHeight
if scroll is @scrollHeaderPos
return
# translateY: headerHeight .. 0
@scrollPageHeader.css('transform', "translateY(#{scroll - headerHeight}px)")
@scrollHeaderPos = scroll
render: (local) =>
# update taskbar with new meta data
App.TaskManager.touch(@taskKey)
if !@renderDone
@renderDone = true
@autosaveLast = {}
elLocal = $(App.view('ticket_zoom')
ticket: @ticket
nav: @nav
isCustomer: @permissionCheck('ticket.customer')
scrollbarWidth: App.Utils.getScrollBarWidth()
dir: App.i18n.dir()
)
new App.TicketZoomOverviewNavigator(
el: elLocal.find('.js-overviewNavigatorContainer')
ticket_id: @ticket_id
overview_id: @overview_id
)
new App.TicketZoomTitle(
object_id: @ticket_id
overview_id: @overview_id
el: elLocal.find('.js-ticketTitleContainer')
taskKey: @taskKey
)
new App.TicketZoomMeta(
object_id: @ticket_id
el: elLocal.find('.js-ticketMetaContainer')
)
@attributeBar = new App.TicketZoomAttributeBar(
ticket: @ticket
el: elLocal.find('.js-attributeBar')
overview_id: @overview_id
callback: @submit
taskKey: @taskKey
)
#if @shown
# @attributeBar.start()
@form_id = @taskGet('article').form_id || App.ControllerForm.formId()
@articleNew = new App.TicketZoomArticleNew(
ticket: @ticket
ticket_id: @ticket_id
el: elLocal.find('.article-new')
formMeta: @formMeta
form_id: @form_id
defaults: @taskGet('article')
taskKey: @taskKey
ui: @
callbackFileUploadStart: @submitDisable
callbackFileUploadStop: @submitEnable
)
@highligher = new App.TicketZoomHighlighter(
el: elLocal.find('.js-highlighterContainer')
ticket_id: @ticket_id
)
new App.TicketZoomSetting(
el: elLocal.find('.js-settingContainer')
ticket_id: @ticket_id
)
@articleView = new App.TicketZoomArticleView(
ticket: @ticket
el: elLocal.find('.ticket-article')
ui: @
highligher: @highligher
ticket_article_ids: @ticket_article_ids
form_id: @form_id
)
new App.TicketCustomerAvatar(
object_id: @ticket_id
el: elLocal.find('.ticketZoom-header')
)
@sidebarWidget = new App.TicketZoomSidebar(
el: elLocal
sidebarState: @sidebarState
object_id: @ticket_id
model: 'Ticket'
query: @query
taskGet: @taskGet
taskKey: @taskKey
formMeta: @formMeta
markForm: @markForm
tags: @tags
links: @links
)
# check if autolock is needed
if @Config.get('ticket_auto_assignment') is true
if @ticket.owner_id is 1 && @permissionCheck('ticket.agent') && @ticket.editable('full')
userIdsIgnore = @Config.get('ticket_auto_assignment_user_ids_ignore') || []
if !_.isArray(userIdsIgnore)
userIdsIgnore = [userIdsIgnore]
userIgnored = false
currentUserId = App.Session.get('id')
for userIdIgnore in userIdsIgnore
if userIdIgnore.toString() is currentUserId.toString()
userIgnored = true
break
if userIgnored is false
ticket_auto_assignment_selector = @Config.get('ticket_auto_assignment_selector')
if App.Ticket.selector(@ticket, ticket_auto_assignment_selector['condition'])
assign = =>
@ticket.owner_id = App.Session.get('id')
@ticket.save()
@delay(assign, 800, "ticket-auto-assign-#{@ticket.id}")
# render init content
if elLocal
@html elLocal
# show article
else
@articleView.execute(
ticket_article_ids: @ticket_article_ids
)
if @sidebarWidget
@sidebarWidget.reload(
tags: @tags
links: @links
)
if !@initDone
if @article_id
@pagePositionData = @article_id
@pagePosition(type: 'init')
@positionPageHeaderStart()
@initDone = true
return
return if local
@pagePosition(type: 'init')
scrollToArticle: (article_id) =>
articleContainer = document.getElementById("article-#{article_id}")
return if !articleContainer
distanceToTop = articleContainer.offsetTop - 100
#@main.scrollTop(distanceToTop)
@main.animate(scrollTop: distanceToTop, 100)
scrollToBottom: =>
# because of .ticketZoom { min-: 101% } (force to show scrollbar to set layout correctly),
# we need to check if we need to really scroll bottom, in case of content isn't really 100%,
# just return (otherwise just a part of movable header is shown down)
realContentHeight = 0
realContentHeight += @$('.ticketZoom-controls').height()
realContentHeight += @$('.ticketZoom-header').height()
realContentHeight += @$('.ticket-article').height()
realContentHeight += @$('.article-new').height()
viewableContentHeight = @$('.main').height()
if viewableContentHeight > realContentHeight
@main.scrollTop(0)
return
@main.scrollTop( @main.prop('scrollHeight') )
autosaveStop: =>
@clearDelay('ticket-zoom-form-update')
@autosaveLast = {}
@el.off('change.local blur.local keyup.local paste.local input.local')
autosaveStart: =>
@el.on('change.local blur.local keyup.local paste.local input.local', 'form, .js-textarea', (e) =>
@delay(@markForm, 250, 'ticket-zoom-form-update')
)
@delay(@markForm, 800, 'ticket-zoom-form-update')
markForm: (force) =>
if !@autosaveLast
@autosaveLast = @taskGet()
return if !@ticket
currentParams = @formCurrent()
# check changed between last autosave
sameAsLastSave = _.isEqual(currentParams, @autosaveLast)
return if !force && sameAsLastSave
@autosaveLast = clone(currentParams)
# update changes in ui
currentStore = @currentStore()
modelDiff = @formDiff(currentParams, currentStore)
# set followup state if needed
@setDefaultFollowUpState(modelDiff, currentStore)
@markFormDiff(modelDiff)
@taskUpdateAll(modelDiff)
currentStore: =>
return if !@ticket
currentStoreTicket = @ticket.attributes()
delete currentStoreTicket.article
internal = @Config.get('ui_ticket_zoom_article_note_new_internal')
currentStore =
ticket: currentStoreTicket
article:
to: ''
cc: ''
subject: ''
type: 'note'
body: ''
internal: internal
in_reply_to: ''
subtype: ''
if @permissionCheck('ticket.customer')
currentStore.article.internal = ''
currentStore
setDefaultFollowUpState: (modelDiff, currentStore) ->
# if the default state is set
# and the body get changed to empty
# then we want to reset the state
if @isDefaultFollowUpStateSet && !modelDiff.article.body
@$('.sidebar select[name=state_id]').val(currentStore.ticket.state_id).trigger('change')
@isDefaultFollowUpStateSet = false
return
# set default if body is filled
return if !modelDiff.article.body
# and state got not changed
return if modelDiff.ticket.state_id
# and we are in the customer interface
return if !@permissionCheck('ticket.customer')
# and the default is was not set before
return if @isDefaultFollowUpStateSet
# and only if ticket is not in "new" state
if @ticket && @ticket.state_id
state = App.TicketState.findByAttribute('id', @ticket.state_id)
return if state && state.default_create is true
# prevent multiple changes for the default follow-up state
@isDefaultFollowUpStateSet = true
# get state
state = App.TicketState.findByAttribute('default_follow_up', true)
# change ui and trigger change
if state
@$('.sidebar[data-tab=ticket] select[name=state_id]').val(state.id).trigger('change')
true
resetDefaultFollowUpState: ->
@isDefaultFollowUpStateSet = false
formCurrent: =>
currentParams =
ticket: @formParam(@el.find('.edit'))
article: @articleNew.params()
# add attachments if exist
attachmentCount = @$('.article-add .textBubble .attachments .attachment').length
if attachmentCount > 0
currentParams.article.attachments = true
else
delete currentParams.article.attachments
delete currentParams.article.form_id
if @permissionCheck('ticket.customer')
currentParams.article.internal = ''
currentParams
formDiff: (currentParams, currentStore) ->
# do not compare null or undefined value
if currentStore.ticket
for key, value of currentStore.ticket
if value is null || value is undefined
currentStore.ticket[key] = ''
if currentParams.ticket
for key, value of currentParams.ticket
if value is null || value is undefined
currentParams.ticket[key] = ''
# get diff of model
modelDiff =
ticket: @forRemoveMeta(App.Utils.formDiff(currentParams.ticket, currentStore.ticket))
article: @forRemoveMeta(App.Utils.formDiff(currentParams.article, currentStore.article))
modelDiff
forRemoveMeta: (params = {}) ->
paramsNew = {}
for key, value of params
if !key.match(/_completion$/)
paramsNew[key] = value
paramsNew
markFormDiff: (diff = {}) =>
ticketForm = @$('.edit')
ticketSidebar = @$('.tabsSidebar-tab[data-tab="ticket"]')
articleForm = @$('.article-add')
resetButton = @$('.js-reset')
params = {}
params.ticket = @forRemoveMeta(@formParam(ticketForm))
params.article = @forRemoveMeta(@formParam(articleForm))
# clear all changes
if _.isEmpty(diff.ticket) && _.isEmpty(diff.article)
ticketSidebar.removeClass('is-changed')
ticketForm.removeClass('form-changed')
ticketForm.find('.form-group').removeClass('is-changed')
resetButton.addClass('hide')
# set changes
else
ticketForm.addClass('form-changed')
if !_.isEmpty(diff.ticket)
ticketSidebar.addClass('is-changed')
else
ticketSidebar.removeClass('is-changed')
for currentKey, currentValue of params.ticket
element = @$('.edit [name="' + currentKey + '"]').parents('.form-group')
if !element.get(0)
element = @$('.edit [data-name="' + currentKey + '"]').parents('.form-group')
if currentKey of diff.ticket
if !element.hasClass('is-changed')
element.addClass('is-changed')
else
if element.hasClass('is-changed')
element.removeClass('is-changed')
resetButton.removeClass('hide')
submitDisable: (e) =>
if e
@formDisable(e)
return
@formDisable(@$('.js-submitDropdown'))
submitEnable: (e) =>
if e
@formEnable(e)
return
@formEnable(@$('.js-submitDropdown'))
submit: (e, macro = {}) =>
e.stopPropagation()
e.preventDefault()
# disable form
@submitDisable(e)
# validate new article
if !@articleNew.validate()
@submitEnable(e)
return
ticketParams = @formParam(@$('.edit'))
articleParams = @articleNew.params()
# validate ticket
# we need to use the full ticket because
# the time accouting needs all attributes
# for condition check
ticket = App.Ticket.fullLocal(@ticket_id)
# reset article - should not be resubmitted on next ticket update
ticket.article = undefined
# update ticket attributes
for key, value of ticketParams
ticket[key] = value
if macro.perform
App.Ticket.macro(
macro: macro.perform
ticket: ticket
article: articleParams
callback:
tagAdd: (tag) =>
return if !@sidebarWidget
return if !@sidebarWidget.reload
@sidebarWidget.reload(tagAdd: tag, source: 'macro')
tagRemove: (tag) =>
return if !@sidebarWidget
return if !@sidebarWidget.reload
@sidebarWidget.reload(tagRemove: tag)
)
# set defaults
if !@permissionCheck('ticket.customer')
if !ticket['owner_id']
ticket['owner_id'] = 1
# if title is empty - ticket can't processed, set ?
if _.isEmpty(ticket.title)
ticket.title = '-'
# stop autosave
@autosaveStop()
# no form validation if macro is performed
if !macro.perform
# validate ticket form using HTML5 validity check
element = @$('.edit').parent().get(0)
if element && element.reportValidity && !element.reportValidity()
@submitEnable(e)
@autosaveStart()
return
# validate ticket by model
errors = ticket.validate(
screen: 'edit'
)
if errors
@log 'error', 'update', errors
@formValidate(
form: @$('.edit')
errors: errors
screen: 'edit'
)
@submitEnable(e)
@autosaveStart()
return
if articleParams && articleParams.body
article = new App.TicketArticle
article.load(articleParams)
errors = article.validate()
if errors
@log 'error', 'update article', errors
@formValidate(
form: @$('.article-add')
errors: errors
screen: 'edit'
)
@submitEnable(e)
@autosaveStart()
return
ticket.article = article
# add sidebar params
if @sidebarWidget && @sidebarWidget.postParams
@sidebarWidget.postParams(ticket: ticket)
if !ticket.article
@submitPost(e, ticket, macro)
return
# verify if time accounting is enabled
if @Config.get('time_accounting') isnt true
@submitPost(e, ticket, macro)
return
# verify if time accounting is active for ticket
time_accounting_selector = @Config.get('time_accounting_selector')
if !App.Ticket.selector(ticket, time_accounting_selector['condition'])
@submitPost(e, ticket, macro)
return
# time tracking
if @permissionCheck('ticket.customer')
@submitPost(e, ticket, macro)
return
new App.TicketZoomTimeAccounting(
container: @el.closest('.content')
ticket: ticket
cancelCallback: =>
@submitEnable(e)
submitCallback: (params) =>
if params.time_unit
ticket.article.time_unit = params.time_unit
@submitPost(e, ticket, macro)
)
submitPost: (e, ticket, macro) =>
taskAction = @$('.js-secondaryActionButtonLabel').data('type')
if macro && macro.ux_flow_next_up
taskAction = macro.ux_flow_next_up
nextTicket = undefined
if taskAction is 'closeNextInOverview' || taskAction is 'next_from_overview'
nextTicket = @getNextTicketInOverview()
# submit changes
@ajax(
id: "ticket_update_#{ticket.id}"
type: 'PUT'
url: "#{App.Ticket.url}/#{ticket.id}?all=true"
data: JSON.stringify(ticket.attributes())
processData: true
success: (data) =>
#App.SessionStorage.set(@key, data)
@load(data, true, true)
# reset article - should not be resubmitted on next ticket update
ticket.article = undefined
# reset form after save
@reset()
if @sidebarWidget
@sidebarWidget.commit()
if taskAction is 'closeNextInOverview' || taskAction is 'next_from_overview'
@openTicketInOverview(nextTicket)
App.Event.trigger('overview:fetch')
return
if taskAction is 'closeTab' || taskAction is 'next_task'
App.Event.trigger('overview:fetch')
@taskCloseTicket(true)
return
@autosaveStart()
@muteTask()
@submitEnable(e)
@scrollToPosition('bottom', 50)
error: (settings, details) =>
error = undefined
if settings && settings.responseJSON && settings.responseJSON.error
error = settings.responseJSON.error
App.Event.trigger 'notify', {
type: 'error'
msg: App.i18n.translateContent(details.error_human || details.error || error || 'Unable to update!')
timeout: 2000
}
@autosaveStart()
@muteTask()
@fetch()
@submitEnable(e)
)
bookmark: (e) ->
$(e.currentTarget).find('.bookmark.icon').toggleClass('filled')
reset: (e) =>
if e
e.preventDefault()
# reset task
@taskReset()
# reset default follow-up state
@resetDefaultFollowUpState()
# reset/delete uploaded attachments
App.Ajax.request(
type: 'DELETE'
url: "#{App.Config.get('api_path')}/upload_caches/#{@form_id}"
processData: false
)
# hide reset button
@$('.js-reset').addClass('hide')
# reset edit ticket / reset new article
App.Event.trigger('ui::ticket::taskReset', { ticket_id: @ticket_id })
# remove change flag on tab
@$('.tabsSidebar-tab[data-tab="ticket"]').removeClass('is-changed')
taskGet: (area) =>
return {} if !App.TaskManager.get(@taskKey)
@localTaskData = App.TaskManager.get(@taskKey).state || {}
if _.isObject(@localTaskData.article) && _.isArray(App.TaskManager.get(@taskKey).attachments)
@localTaskData.article['attachments'] = App.TaskManager.get(@taskKey).attachments
if area
if !@localTaskData[area]
@localTaskData[area] = {}
return @localTaskData[area]
if !@localTaskData
@localTaskData = {}
@localTaskData
taskUpdate: (area, data) =>
@localTaskData[area] = data
App.TaskManager.update(@taskKey, { 'state': @localTaskData })
taskUpdateAll: (data) =>