-
Notifications
You must be signed in to change notification settings - Fork 5
/
feedback.js
280 lines (237 loc) · 8.72 KB
/
feedback.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
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
// Planbox Feedback Widget (http://www.planbox.com/help/user_feedback)
//
// Author: Martin Drapeau
// Copyright: Planbox Inc. 2011-2012
// License: MIT License (http://en.wikipedia.org/wiki/MIT_License)
window.FeedbackOptions = jQuery.extend({
// You can find your Planbox initiative token on the Manage page
// Instructions: http://www.planbox.com/help/user_feedback
planboxToken: '<TOKEN>',
// You can forward any incoming feedback to an email
// Keep empty not to. Only a single email is allowed.
forwardEmail: '',
// You can set the page URL. If empty will use window.location.href
pageUrl: '',
// You can specify a human readable page title
pageTitle: '',
// The button appears center-left by default. Modify in CSS.
// It is an image you can modify here.
// If you want your own custom button, set showButton to false.
// You can then call FeedbackShow().
showButton: true,
buttonImageSrc: document.location.protocol+'//www.planbox.com/img/feedback_button.png',
buttonImageAlt: 'Feedback',
buttonTooltip: 'Send us Feedback',
// When you click on the button it opens a dialog
dialogTitle: 'User Feedback',
dialogCloseTooltip: 'Close dialog',
dialogEmailLabel: 'Your email',
dialogFeedbackLabel: 'Your feedback',
dialogCancelText: 'Cancel',
dialogSubmitText: 'Submit',
dialogCloseText: 'Close',
// You can prefill the email
dialogEmail: '',
// When the user clicks on Submit, do some validation
// and tell the user what's happening
emailIsRequired: true,
emailInvalidError: 'Please provide a valid email',
feedbackMissingError: 'Please type in feedback',
feedbackAjaxSuccess: 'We have received your feedback. Thank you!',
feedbackAjaxError: 'There was an error.',
// When the user opens the window
// The dialog element is passed as argument
onshow: function(){},
// Deprecated...
// When the user opens the window
// The jQuery content element is passed as argument
callback: function(){}
}, window.FeedbackOptions || {});
jQuery(document).ready(function() {
var $ = jQuery;
var options = window.FeedbackOptions;
var button_em;
if (options.showButton) {
// Create the button and hide it
button_em = $('<div id="feedback_button">').appendTo('body').hide();
button_em.attr('title', options.buttonTooltip);
$('<img src="'+options.buttonImageSrc+'" alt="Feedback" />').appendTo(button_em);
// Extend the button if hovered
button_em.hover(
function() {
$(this).stop().animate({paddingLeft:'5'});
},
function() {
$(this).stop().animate({paddingLeft:'0'});
}
);
}
// Show the feedback dialog when the button is clicked
window.FeedbackShow = function(e) {
// Create a dialog
var mask_em = $('<div id="feedback_mask">').appendTo('body');
var dialog_em = $('<div id="feedback_dialog">').appendTo('body');
// Header of the dialog
var header_em = $('<div class="header">').appendTo(dialog_em);
header_em.text(options.dialogTitle);
var close_em = $('<a href="#" class="close" title="'+options.dialogCloseTooltip+'">×</a>').appendTo(header_em);
// Allow drag and drop of the dialog through its header
header_em.mousedown(function(e) {
var pos = dialog_em.position();
if (e.target == close_em[0]) return false;
header_em.data('dnd', {
mouseX: e.clientX,
mouseY: e.clientY,
elX: pos.left,
elY: pos.top
});
$(document).bind('mouseup.dialog_dnd', function(e) {
$(this).unbind('.dialog_dnd');
header_em.removeData('dnd');
return false;
});
$(document).bind('mousemove.dialog_dnd', function(e) {
var dnd = header_em.data('dnd');
if (!dnd) return true;
dialog_em.css({
left: dnd.elX + (e.clientX - dnd.mouseX),
top: dnd.elY + (e.clientY - dnd.mouseY)
});
if (options.onresize) options.onresize.apply(dialog_em);
return false;
});
return true;
});
// Prevent text selection in header
header_em.bind('selectstart', function(e) {
return false;
});
// Content
var tabindex = 100;
var content_em = $('<div class="content"></div>').appendTo(dialog_em);
// Email input
$('<label class="email">'+options.dialogEmailLabel+'</label>').appendTo(content_em);
var email_em = $('<input class="email" type="text" tabindex="'+(tabindex++)+'" value="'+options.dialogEmail+'"/>').appendTo(content_em);
// Feedback textarea
$('<label class="feedback">'+options.dialogFeedbackLabel+'</label>').appendTo(content_em);
var feedback_em = $('<textarea class="feedback" tabindex="'+(tabindex++)+'"></textarea>').appendTo(content_em);
// AJAX message
var ajax_em = $('<p class="ajax"> </p>').appendTo(content_em);
// Button pane
var buttons_em = $('<div class="buttons"></div>').appendTo(dialog_em);
// Powered by Planbox
$('<a class="powered_by_planbox" href="http://www.planbox.com" target="_blank"><img src="'+document.location.protocol+'//www.planbox.com/img/powered_by_planbox.png" alt="Powered by Planbox" /></a>').appendTo(buttons_em);
var apply_em = $('<a class="fr button apply" href="#" tabindex="'+(tabindex+2)+'">'+options.dialogSubmitText+'</a>').appendTo(buttons_em);
var cancel_em = $('<a class="fr no_button cancel" href="#" tabindex="'+(tabindex+1)+'">'+options.dialogCancelText+'</a>').appendTo(buttons_em);
// Function to hide the dialog
var _hide = function(e) {
$(document).unbind('.feedback_dialog');
dialog_em.remove();
mask_em.remove();
return false;
};
// Function to submit the feedback to Planbox
var _submit = function(e) {
ajax_em
.stop(true, true)
.removeClass('error ok')
.empty();
// If email is required and not passed or invliad, show
// an error message and eventually make it disapear
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var email = $.trim(email_em.val());
if ((options.emailIsRequired && email.length == 0) ||
(email.length > 0 && !re.test(email))) {
ajax_em
.addClass('error')
.html(options.emailInvalidError)
.delay(3000).fadeOut(1000, function() {
$(this).empty().show();
});
return false;
}
// If feedback is not passed, show an error message and
// eventually make it disapear
var feedback = $.trim(feedback_em.val());
if (feedback.length == 0) {
ajax_em
.addClass('error')
.html(options.feedbackMissingError)
.delay(3000).fadeOut(1000, function() {
$(this).html(' ').removeClass('error ok').show();
});
return false;
}
apply_em.hide();
// Send the feedback using JSONP to avoid cross domain scripting issues
$.getJSON(document.location.protocol+'//www.planbox.com/api/feedback?callback=?', {
token: options.planboxToken,
feedback: feedback,
user_email: email,
forward_email: options.forwardEmail,
page_title: (options.pageTitle && options.pageTitle.length)?options.pageTitle:'',
page_url: (options.pageUrl && options.pageUrl.length)?options.pageUrl:window.location.href
}, function(data) {
if (data && data.code) {
if (data.code == 'ok') {
ajax_em.addClass('ok').html(options.feedbackAjaxSuccess);
cancel_em.text(options.dialogCloseText);
} else {
ajax_em.addClass('error').html(options.feedbackAjaxError+' '+data.content);
apply_em.show();
}
} else {
ajax_em.addClass('error').html(options.feedbackAjaxError);
apply_em.show();
}
}
);
return false;
};
// Apply button
apply_em.click(_submit);
apply_em.keydown(function(e) {
if (e.keyCode == 13) return _submit(e);
return true;
});
// Cancel button
cancel_em.click(_hide);
cancel_em.keydown(function(e) {
if (e.keyCode == 13) return _hide();
return true;
});
// Show the mask and dialog
mask_em.show();
dialog_em.show();
close_em.click(_hide);
// Position the dialog in the center and show
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var x = Math.round($(document).scrollLeft() + $(window).width()/2 - dialog_em.width()/2);
var y = Math.round($(document).scrollTop() + $(window).height()/2 - dialog_em.height()/1.5);
if (x < 10) x = 10;
if (y < 100) y = 100;
dialog_em.css({'left': x, 'top': y});
// Bind Esc to close dialog
$(document).bind('keydown.feedback_dialog', function(e) {
if (e.keyCode == 27) {
e.stopImmediatePropagation();
return _hide(e);
}
return true;
});
// Deprecated. Replaced by onshow
if (options.callback)
options.callback(content_em);
// Trigger the onshow callback
if (options.onshow)
options.onshow(dialog_em[0]);
return false;
};
if (button_em) {
// Trigger show on button click
button_em.click(window.FeedbackShow);
// Show the button
button_em.show();
}
});