-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (97 loc) · 3.16 KB
/
Copy pathapp.js
File metadata and controls
107 lines (97 loc) · 3.16 KB
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
(function() {
var appParentSpace = this;
return {
events: {
'app.activated': 'getRequesterLanguage',
'ticket.save': 'checkLanguage'
},
requests: {
detectLanguage: function(commentText) {
return {
url: 'http://ws.detectlanguage.com/0.2/detect?q=' + encodeURI(commentText).replace(/%20/g, '+') + '&key=6371fd05809a4dc1f5b3bb1f67500924',
dataType: 'JSON',
type: 'GET',
contentType: 'application/json',
proxy_v2: true
};
},
getLocaleID: function(userID) {
return {
url: '/api/v2/users/' + userID + '.json',
dataType: 'JSON',
type: 'GET',
contentType: 'application/json',
proxy_v2: true
};
}
},
getRequesterLanguage: function() {
var ticket = this.ticket();
var user = ticket.requester();
return this.promise(function(done, fail) {
var comment = this.comment();
this.ajax('getLocaleID', user.id())
.then(function(jsonResponse) {
console.log("Requester locale: " + jsonResponse.user.locale);
this.requesterLocale = jsonResponse.user.locale;
done();
}, function() {
console.log("Request Failed");
fail();
});
});
},
checkLanguage: function() {
return this.promise(function(done, fail) {
var comment = this.comment();
this.ajax('detectLanguage', comment.text())
.then(function(jsonResponse) {
if (this.setting(this.requesterLocale) === undefined) {
this.transformedLocale = this.requesterLocale;
} else {
this.transformedLocale = this.setting(this.requesterLocale);
}
var json = this.setting("locales");
locales = JSON.parse(json);
console.log("Comment language detected: " + jsonResponse.data.detections[0].language);
this.store('dLanguage', locales[jsonResponse.data.detections[0].language]);
console.log("Requester locale transformed: " + this.transformedLocale);
this.store('rLanguage', locales[this.transformedLocale]);
if (this.transformedLocale != jsonResponse.data.detections[0].language) {
console.log("Doesn't match.");
this.$('#myModal').modal();
var self = this;
self.$('.modal_submit').bind("click", function(clicked) {
clicked.preventDefault();
if (clicked.target.id == "modal_submit_continue") {
console.log("Clicked continue.");
appParentSpace.$('#ht_modal').modal('hide');
appParentSpace.$('.modal-backdrop').remove();
done();
} else if (clicked.target.id == "modal_submit_close") {
console.log("Clicked cancel.");
fail(self.I18n.t('submit.cancel'));
}
});
self.$('.modal-header button.close').bind("click", function() {
console.log("Clicked close.");
fail(self.I18n.t('submit.close'));
});
setTimeout(function() {
fail(self.I18n.t('submit.timeout'));
appParentSpace.$('#myModal').modal('hide');
appParentSpace.$('.modal-backdrop').remove();
}, 20000);
} else {
console.log("Match.");
done();
}
},
function() {
console.log("Request Failed");
fail();
});
});
}
};
}());