pandora/static/js/pandora/contactForm.js

181 lines
6.3 KiB
JavaScript
Raw Normal View History

2011-10-06 15:39:28 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-11-02 10:22:19 +00:00
2011-11-05 17:04:10 +00:00
'use strict';
2011-10-06 15:39:28 +00:00
pandora.ui.contactForm = function() {
2011-11-02 10:22:19 +00:00
2011-10-06 15:39:28 +00:00
var that = Ox.Element(),
2011-11-02 10:22:19 +00:00
width = getWidth(),
2011-10-06 15:39:28 +00:00
$form = Ox.Form({
items: [
Ox.Input({
2011-11-02 10:22:19 +00:00
id: 'name',
label: 'Your Name',
labelWidth: 128,
2011-11-02 17:26:08 +00:00
validate: function(value, callback) {
callback({valid: true});
},
2011-11-02 10:22:19 +00:00
value: pandora.user.username,
width: width
2011-10-06 15:39:28 +00:00
}),
Ox.Input({
2011-11-02 17:26:08 +00:00
autovalidate: pandora.autovalidateEmail,
2011-11-02 10:22:19 +00:00
id: 'email',
label: 'Your E-Mail Address',
labelWidth: 128,
2011-11-02 17:26:08 +00:00
validate: function(value, callback) {
callback({
message: 'Please enter '
+ (value.length == 0 ? 'your' : 'a valid')
+ ' e-mail address',
valid: Ox.isValidEmail(value)
});
},
2011-11-02 10:22:19 +00:00
value: pandora.user.email,
width: width
2011-10-06 15:39:28 +00:00
}),
Ox.Input({
2011-11-02 10:22:19 +00:00
id: 'subject',
label: 'Subject',
labelWidth: 128,
2011-11-02 17:26:08 +00:00
validate: function(value, callback) {
callback({valid: true});
},
2011-11-02 10:22:19 +00:00
value: '',
width: width
}),
Ox.Input({
2011-11-02 17:26:08 +00:00
autovalidate: /.+/,
2011-11-02 10:22:19 +00:00
height: 256,
2011-10-06 15:39:28 +00:00
id: 'message',
placeholder: 'Message',
type: 'textarea',
2011-11-02 17:26:08 +00:00
validate: function(value, callback) {
callback({
message: 'Please enter a message',
valid: value.length > 0
});
},
2011-10-06 15:39:28 +00:00
value: '',
2011-11-02 10:22:19 +00:00
width: width
2011-11-02 17:26:08 +00:00
})
2011-10-06 15:39:28 +00:00
],
2011-11-02 17:26:08 +00:00
})
.css({width: width + 'px'})
.bindEvent({
validate: function(data) {
$sendButton.options({
disabled: !data.valid
2011-10-06 15:39:28 +00:00
});
2011-11-02 17:26:08 +00:00
}
})
.appendTo(that),
$receiptCheckbox = Ox.Checkbox({
checked: pandora.user.level != 'guest',
id: 'receipt',
title: 'Send a receipt to ' + pandora.user.email,
width: width - 136
2011-10-06 15:39:28 +00:00
})
2011-11-02 17:26:08 +00:00
.css({float: 'left', margin: '8px 4px 8px 0'})
2011-10-06 15:39:28 +00:00
.bindEvent({
2011-11-02 17:26:08 +00:00
change: function(data) {
$receiptCheckbox.options({
title: data.checked
? 'Send a receipt to ' + pandora.user.email
: 'Don\'t send me a receipt'
});
2011-10-06 15:39:28 +00:00
}
})
2011-11-02 10:22:19 +00:00
.appendTo(that),
$sendButton = Ox.Button({
2011-11-02 17:26:08 +00:00
disabled: true,
2011-11-02 10:22:19 +00:00
title: 'Send Message',
width: 128
})
2011-11-02 17:26:08 +00:00
.css({float: 'left', margin: '8px 0 8px ' + (pandora.user.level == 'guest' ? width - 128 : 4) + 'px'})
2011-11-02 10:22:19 +00:00
.bindEvent({
click: function() {
2011-11-02 17:26:08 +00:00
var data = $form.values();
pandora.api.contact({
name: data.name,
email: data.email,
subject: data.subject,
message: data.message,
receipt: $receiptCheckbox.value()
}, function(result) {
var $dialog = Ox.Dialog({
buttons: [
Ox.Button({
id: 'close',
title: 'Close'
}).bindEvent({
click: function() {
$dialog.close();
}
})
],
content: Ox.Element()
.append(
$('<img>')
.attr({src: '/static/png/icon64.png'})
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
Ox.Element()
.css({position: 'absolute', left: '96px', top: '16px', width: '192px'})
.html('Thanks for your message!<br/><br/>We will get back to you as soon as possible.')
),
fixedSize: true,
height: 128,
keys: {enter: 'close', escape: 'close'},
title: 'Message Sent',
width: 304
})
.open();
});
2011-11-02 10:22:19 +00:00
}
})
2011-10-06 15:39:28 +00:00
.appendTo(that);
2011-11-02 10:22:19 +00:00
$text = $('<div>')
2011-11-02 17:26:08 +00:00
.css({width: width + 'px'})
2011-11-02 10:22:19 +00:00
.html(
2011-11-02 17:26:08 +00:00
'&nbsp;Alternatively, you can contact us via <a href="mailto:'
2011-11-02 10:22:19 +00:00
+ pandora.site.site.email.contact + '">'
+ pandora.site.site.email.contact + '</a>'
)
.appendTo(that);
2011-11-02 17:26:08 +00:00
pandora.user.level == 'guest' && $receiptCheckbox.hide();
2011-11-02 10:22:19 +00:00
function getWidth() {
return Math.min((
pandora.$ui.siteDialog
? parseInt(pandora.$ui.siteDialog.css('width'))
: Math.round(window.innerWidth * 0.75)
) - 304 - Ox.UI.SCROLLBAR_SIZE, 512);
2011-11-02 10:22:19 +00:00
}
that.resize = function() {
var width = getWidth();
2011-11-02 17:26:08 +00:00
$form.css({width: width + 'px'});
2011-11-02 10:22:19 +00:00
$form.options('items').forEach(function($input, i) {
i < 4 && $input.options({width: width});
});
2011-11-02 17:26:08 +00:00
if (pandora.user.level == 'guest') {
$sendButton.css({marginLeft: width - 128 + 'px'});
} else {
$receiptCheckbox.options({width: width - 136});
}
2011-11-02 10:22:19 +00:00
$text.css({width: width + 'px'});
}
2011-10-06 15:39:28 +00:00
return that;
2011-11-02 10:22:19 +00:00
2011-10-06 15:39:28 +00:00
};