This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
app.js
102 lines (83 loc) · 3.1 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(function() {
return {
requests: {
fetchUsers: function(ids) {
return {
url: helpers.fmt('/api/v2/users/show_many.json?ids=%@&include=organizations,groups', ids.join(',')),
type: 'GET',
dataType: 'json'
};
}
},
events: {
'app.created' : 'onAppCreated',
'*.changed' : function(e) {
if (_.contains(this.fieldsToWatch(), e.propertyName))
return this.onAppCreated();
},
'fetchUsers.done' : 'onFetchUsersDone'
},
onAppCreated: function() {
var userIds = _.compact(_.uniq([
(this.ticket().assignee().user() && this.ticket().assignee().user().id()),
this.currentUser().id(),
(this.ticket().requester() && this.ticket().requester().id())
]));
this.ajax('fetchUsers', userIds);
},
onFetchUsersDone: function(data) {
var templateUris = this.getUriTemplatesFromSettings(),
templateOptions = { interpolate : /\{\{(.+?)\}\}/g },
context = this.getContext(data),
uris = _.map(templateUris, function(uri){
try {
uri.url = _.template(uri.url, templateOptions)(context);
uri.title = _.template(uri.title, templateOptions)(context);
} catch(e) {
services.notify(e, 'error');
}
return uri;
}, this);
this.switchTo('list', { uris: uris });
},
getUriTemplatesFromSettings: function(){
return JSON.parse(this.settings.uri_templates);
},
getContext: function(data){
var context = _.clone(this.containerContext());
if (context.ticket.requester.id) {
context.ticket.requester = this.decorateUser(this.findUserById(data.users, context.ticket.requester.id));
if (context.ticket.requester.organization_id) {
context.ticket.organization = _.find(data.organizations, function(org) {
return org.id == context.ticket.requester.organization_id;
});
}
}
if (context.ticket.assignee.user.id) {
context.ticket.assignee.user = this.decorateUser(this.findUserById(data.users, context.ticket.assignee.user.id));
}
//copying data not available on this.containerContext()
context.ticket.id = this.ticket().id();
context.ticket.description = this.ticket().description();
context.current_user = this.decorateUser(this.findUserById(data.users, this.currentUser().id()));
return context;
},
findUserById: function(users, user_id) {
return _.find(users, function(user) {
return user.id == user_id;
}, this);
},
decorateUser: function(user){
var name = (user.name || '').split(' ');
user.firstname = name[0] || '';
user.lastname = name[1] || '';
return user;
},
fieldsToWatch: _.memoize(function(){
return _.reduce(this.getUriTemplatesFromSettings(), function(memo, uri){
var fields = _.map(uri.url.match(/\{\{(.+?)\}\}/g), function(f){ return f.slice(2,-2); });
return _.union(memo, fields);
}, []);
})
};
}());