This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
80 lines (69 loc) · 2.74 KB
/
app.js
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
(function() {
'use strict';
var oauth_url = 'https://domain.tld/google_login/';
var fields = {
start_date: 'custom_field_24462829',
start_time: 'custom_field_24435179',
end_date: 'custom_field_24524155',
end_time: 'custom_field_24500385'
};
return {
can_sync: false,
events: {
'app.activated': 'init',
'app.registered': 'ticket_location',
'iframe.datetime': 'update_fields',
// events below need same ids as in fields object
'ticket.custom_field_24462829.changed': 'send_data',
'ticket.custom_field_24435179.changed': 'send_data',
'ticket.custom_field_24524155.changed': 'send_data',
'ticket.custom_field_24500385.changed': 'send_data'
},
update_fields: function(data) {
var ticket = this.ticket();
ticket.customField(fields.start_date, data.start_date);
ticket.customField(fields.start_time, data.start_time);
ticket.customField(fields.end_date, data.end_date);
ticket.customField(fields.end_time, data.end_time);
},
datefield_to_iso: function(field) {
var ticket = this.ticket();
var date = moment(Date.parse(ticket.customField(field))).format('YYYY-MM-DD');
return date !== 'Invalid date' ? date : null;
},
send_data: function() {
// make sure the app.registered has fired
if(!this.can_sync) {
return;
}
var ticket = this.ticket();
this.postMessage('datetime', {
start_date: this.datefield_to_iso(fields.start_date),
start_time: ticket.customField(fields.start_time),
end_date: this.datefield_to_iso(fields.end_date),
end_time: ticket.customField(fields.end_time)
});
},
ticket_location: function() {
this.can_sync = true;
// https://github.com/zendesk/zendesk_app_framework_sdk/issues/7
var location = this.currentLocation();
if(location === 'new_ticket_sidebar') {
this.postMessage('new_ticket');
} else if(location === 'ticket_sidebar') {
this.send_data();
}
},
init: function() {
var location = this.currentLocation();
if(location === 'nav_bar') {
this.switchTo('readme', {
oauth_url: oauth_url + this.currentUser().id()
});
} else if(location === 'ticket_sidebar' ||
location === 'new_ticket_sidebar') {
this.switchTo('ticket');
}
}
};
}());