2012-10-18 20:29:16 +02:00
|
|
|
var compose = (function () {
|
2012-10-03 22:53:15 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.show = function (tabname, focus_area) {
|
2012-10-17 20:43:20 +02:00
|
|
|
if (reloading_app) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-10 22:57:02 +02:00
|
|
|
$("#send-status").removeClass(status_classes).hide();
|
2012-10-09 23:56:16 +02:00
|
|
|
$('#compose').css({visibility: "visible"});
|
2012-10-10 00:06:04 +02:00
|
|
|
$('.message_comp').slideDown(100);
|
2012-10-09 23:46:41 +02:00
|
|
|
$('#message-type-tabs a[href="#' + tabname + '-message"]').tab('show');
|
2012-10-04 00:04:58 +02:00
|
|
|
focus_area.focus();
|
|
|
|
focus_area.select();
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-04 00:04:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide = function () {
|
2012-10-04 00:04:58 +02:00
|
|
|
$('input, textarea, button').blur();
|
2012-10-10 00:06:04 +02:00
|
|
|
$('.message_comp').slideUp(100,
|
2012-10-09 23:56:16 +02:00
|
|
|
function() { $('#compose').css({visibility: "hidden"});});
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-04 00:04:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.clear = function () {
|
2012-10-09 23:56:16 +02:00
|
|
|
$("#compose").find('input[type=text], textarea').val('');
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 21:44:07 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.button_press = function (tabname) {
|
|
|
|
exports.clear();
|
2012-10-03 20:49:58 +02:00
|
|
|
$('#sidebar a[href="#home"]').tab('show');
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.show(tabname, $("#" + tabname));
|
2012-10-18 19:58:10 +02:00
|
|
|
hotkeys.set_compose();
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.toggle_mode = function () {
|
2012-10-10 23:29:02 +02:00
|
|
|
if ($("#message-type-tabs li.active").find("a[href=#stream-message]").length !== 0) {
|
2012-10-10 23:37:14 +02:00
|
|
|
// In stream tab, switch to personals.
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.show('personal', $("#huddle_recipient"));
|
2012-10-03 20:49:58 +02:00
|
|
|
} else {
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.show('stream', $("#stream"));
|
2012-10-03 20:49:58 +02:00
|
|
|
}
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.composing = function () {
|
|
|
|
if ($("#stream-message").is(":visible")) {
|
|
|
|
return 'stream';
|
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if ($("#personal-message").is(":visible")) {
|
|
|
|
return 'huddle';
|
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
return false;
|
|
|
|
};
|
2012-10-11 20:30:34 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.stream_name = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#stream").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#stream").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.subject = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#subject").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#subject").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.message = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#new_message_content").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#new_message_content").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.recipient = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#huddle_recipient").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#huddle_recipient").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
function compose_error(error_text, bad_input) {
|
|
|
|
$('#send-status').removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.text(error_text)
|
|
|
|
.stop(true).fadeTo(0, 1);
|
2012-10-09 23:56:16 +02:00
|
|
|
$('#compose').find('input[type="submit"]').removeAttr('disabled');
|
2012-10-03 20:49:58 +02:00
|
|
|
bad_input.focus().select();
|
|
|
|
}
|
2012-10-03 22:39:05 +02:00
|
|
|
|
2012-10-03 23:04:36 +02:00
|
|
|
function submit_buttons() {
|
2012-10-09 23:56:16 +02:00
|
|
|
return $('#compose').find('input[type="submit"]');
|
2012-10-03 23:04:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
// *Synchronously* check if a stream exists.
|
2012-10-03 22:53:15 +02:00
|
|
|
// If not, displays an error and returns false.
|
2012-10-10 23:37:14 +02:00
|
|
|
function check_stream_for_send(stream_name) {
|
2012-10-03 22:53:15 +02:00
|
|
|
var okay = true;
|
|
|
|
$.ajax({
|
2012-10-16 22:10:48 +02:00
|
|
|
type: "POST",
|
2012-10-17 20:05:17 +02:00
|
|
|
url: "/json/subscriptions/exists",
|
|
|
|
data: {'stream': stream_name},
|
2012-10-03 22:53:15 +02:00
|
|
|
async: false,
|
|
|
|
success: function (data) {
|
2012-10-18 19:40:53 +02:00
|
|
|
if (!data.exists) {
|
2012-10-10 23:37:14 +02:00
|
|
|
// The stream doesn't exist
|
2012-10-03 22:53:15 +02:00
|
|
|
okay = false;
|
2012-10-04 00:04:58 +02:00
|
|
|
$('#send-status').removeClass(status_classes).show();
|
2012-10-10 23:33:38 +02:00
|
|
|
$('#stream-dne-name').text(stream_name);
|
|
|
|
$('#stream-dne').show();
|
2012-10-03 23:04:36 +02:00
|
|
|
submit_buttons().removeAttr('disabled');
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide();
|
2012-10-09 20:06:56 +02:00
|
|
|
$('#create-it').focus();
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
$("#home-error").hide();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
okay = false;
|
|
|
|
report_error("Error checking subscription", xhr, $("#home-error"));
|
2012-10-10 23:33:38 +02:00
|
|
|
$("#stream").focus();
|
2012-10-03 23:04:36 +02:00
|
|
|
submit_buttons().removeAttr('disabled');
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return okay;
|
|
|
|
}
|
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
function validate_stream_message() {
|
2012-10-18 20:29:16 +02:00
|
|
|
var stream_name = exports.stream_name();
|
2012-10-10 23:28:31 +02:00
|
|
|
if (stream_name === "") {
|
2012-10-10 23:37:14 +02:00
|
|
|
compose_error("Please specify a stream", $("#stream"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.subject() === "") {
|
2012-10-11 00:01:39 +02:00
|
|
|
compose_error("Please specify an subject", $("#subject"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.message() === "") {
|
2012-10-09 23:43:53 +02:00
|
|
|
compose_error("You have nothing to send!", $("#new_message_content"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
if (!subs.have(stream_name)) {
|
2012-10-18 19:26:39 +02:00
|
|
|
if (!check_stream_for_send(stream_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-10 23:37:14 +02:00
|
|
|
// You're not subbed to the stream
|
2012-10-03 23:13:38 +02:00
|
|
|
$('#send-status').removeClass(status_classes).show();
|
2012-10-10 23:33:38 +02:00
|
|
|
$('#stream-nosub-name').text(stream_name);
|
|
|
|
$('#stream-nosub').show();
|
2012-10-03 23:13:38 +02:00
|
|
|
submit_buttons().removeAttr('disabled');
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide();
|
2012-10-09 20:06:56 +02:00
|
|
|
$('#sub-it').focus();
|
2012-10-03 23:13:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:39:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate_huddle_message() {
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.recipient() === "") {
|
2012-10-09 15:36:24 +02:00
|
|
|
compose_error("Please specify at least one recipient", $("#huddle_recipient"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.message() === "") {
|
2012-10-09 23:43:53 +02:00
|
|
|
compose_error("You have nothing to send!", $("#new_message_content"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-03 22:39:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.validate = function () {
|
2012-10-03 23:13:38 +02:00
|
|
|
submit_buttons().attr('disabled', 'disabled').blur();
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.composing() === 'huddle') {
|
2012-10-03 23:13:38 +02:00
|
|
|
return validate_huddle_message();
|
|
|
|
} else {
|
2012-10-10 23:37:14 +02:00
|
|
|
return validate_stream_message();
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|