// $Rev$ - 2K13/CS var default_modal_options = { opacity: 100, position: [100, undefined] }; var default_hover_options = { timeout: 500 }; var default_video_options = { autostart: true, width: 200, height: 200 }; // vertical-centered dialog (default position) when large display if(screen.height >= 1080) default_modal_options["position"] = undefined; $(document).ready(function() { $(document).ajaxError(handle_ajax_error); $("form.register").on("submit", register_form_submit); $("form.reset").on("submit", reset_form_submit); $("form.verify").on("submit", verify_form_submit); $("form.login").on("submit", login_form_submit); $("form.activate").on("submit", activate_form_submit); $("form.approval").on("submit", approval_form_submit); $("form.reply_registered").on("submit", reply_registered_form_submit); $("form.reply_anonymous").on("submit", reply_anonymous_form_submit); $("form#user_update_form").on("submit", user_update_form_submit); $("input#reset_avatar").on("change", user_update_reset_avatar_change); $("a.login_link").on("click", click_login_link); $("a.logout_link").on("click", click_logout_link); $("a.register_link").on("click", click_register_link); $("a.regenerate_captcha_link").on("click", click_regenerate_captcha_link); $("a.update_posting_link").on("click", click_update_posting_link); $("a.quote_posting_link").on("click", click_quote_posting_link); $("a.preview_reply_link").on("click", click_preview_reply_link); $("a.preview_topic_link").on("click", click_preview_topic_link); $("input.poll_answer").on("click", click_poll_answer); $("a.reset_password").on("click", show_reset_password); $("input.submit_on_enter").on("keypress", keypress_submit_on_enter); $("a.submit_search").on("click", click_submit_search); $("a.toggle_markup_example").on("click", click_toggle_markup_example); // register events for dynamic elements on the document $(document).on("submit", "form.dynamic", dynamic_form_submit); $(document).on("click", "a.submit_link", click_submit_link); $(document).on("click", ".close_link", click_close_link); // prepare for default_value_input elements default_value_autohide(); $(document).on("focus", ".default_value_input input", default_value_focus); $(document).on("blur", ".default_value_input input", default_value_blur); $(document).on("click", ".default_value_input span", default_value_click); // give the bots a nice snack to eat (anti-abuse) $.get("/json/botSnack", function() { } ); // auto-show activation dialog when included in document var activation = $("div#activate_dialog"); if(activation.length) { show_modal_dialog(activation, { modal_options: { focus: false }, on_dialog_init: function(dialog) { $("input:password:first", dialog).focus(); return true; } }); } // auto-show verification dialog when included in document var verification = $("div#verification_dialog"); if(verification.length) { show_modal_dialog(verification, { modal_options: { focus: false }, on_dialog_close: function(dialog) { window.location.href = "/"; } }); } }); // invoked before the dom is ready (from the head section) function preload() { } // wrapper for show-modal including xhrgrouping for requests function show_modal_dialog(element, options) { // merge default modal-options with options.modal_options var modal_options = $.extend(true, { }, default_modal_options); if(options.modal_options) $.extend(modal_options, options.modal_options); var active_element = $(document.activeElement); // initialize xhrgroup for element and setup close/init handlers $.xhrgroup.initGroup(element); modal_options.onClose = function() { $.xhrgroup.closeGroup(element); var should_close = options.on_dialog_close ? options.on_dialog_close(element) : true; if(should_close) { $.modal.close(); if(options.after_dialog_close) options.after_dialog_close(element); active_element.focus(); } }; modal_options.onShow = function() { if(options.on_dialog_init) options.on_dialog_init(element); } // modal dialog-open callback is optional (animations mostly) if(options.on_dialog_open) { modal_options.onOpen = function(target) { options.on_dialog_open(element, target); } } else modal_options.onOpen = undefined; return element.modal(modal_options); } // bunch of straighforward on-click event handlers function keypress_submit_on_enter(event) { if(event.keyCode != 13) return true; var form = $(this).closest("form"); if(!(form.length)) return true; form.submit(); event.preventDefault(); return false; // dont propagate the event } function click_submit_search(event) { event.preventDefault(); var form = $(this).closest("form"); if(!(form.length)) return false; form.submit(); return false; // dont propagate the event } function click_toggle_markup_example(event) { event.preventDefault(); var container = $(this).siblings("div.markup_example"); if(!(container.length)) return false; if($(container).is(":visible")) { $(container).fadeOut('fast'); } else $(container).fadeIn('fast'); return false; // dont propagate the event } function click_login_link(event) { event.preventDefault(); return show_modal_dialog($("div#login_dialog"), {}); } function click_register_link(event) { event.preventDefault(); return show_modal_dialog($("div#register_dialog"), {}); } function click_submit_link(event) { event.preventDefault(); return $(this).parents("form").submit(); } function click_logout_link(event) { event.preventDefault(); $.postJSON("/json/logoutUser", { token: mytoken }, function(data) { window.location.reload(); }); } function click_close_link(event) { event.preventDefault(); return $.modal.close(); } // extract posting/warnings from specified preview response // // Note: caller will probably insert result into dom (careful!) function extract_preview_response(response) { var posting = $("div#preview_container div.posting:first", response); var warnings = $("div.warning", response); // return (converted) failure when warnings/no-posting if(warnings.length) { var result = { failure: [] }; warnings.each(function() { result.failure.push($(this).text()); }); return result; } else if(!posting.length) { // should have triggered warnings really var result = { failure: ["Unexpected failure"] }; return result; } // retrun simple success/posting (dom node) object var result = { success: true, posting: posting }; return result; } // cancel active reply preview contained in specified form function cancel_reply_preview(form) { var existing = $("div.posting.preview", form); var textarea = $("textarea[name = 'content']", form); var button = $("a.preview_reply_link", form); if(!(existing.length)) return true; existing.remove(); textarea.fadeIn("fast"); button.removeClass("grey").addClass("green"); $("span", button).text("Preview"); return true; } // toggle preview/edit mode in preview-repy click handler function click_preview_reply_link(event) { event.preventDefault(); var form = $(this).closest("form"); var textarea = $("textarea[name = 'content']", form); var button = $("a.preview_reply_link", form); var existing = $("div.posting.preview", form); // switch-back to edit-mode when existing preview found if(existing.length) { cancel_reply_preview(form); return false; } // fetch the preview for the markup currently in the textarea form_reset_messages(form); $.post("/preview", { token: mytoken, markup: textarea.val(), mode: "reply" }, function(response) { // update client-side view on success var data = extract_preview_response(response); if(data.failure) { form_show_messages(form, data.failure); return false; } var posting = data.posting; posting.addClass("preview"); // insert posting before textarea and hide textarea posting.insertBefore(textarea); textarea.hide(); button.removeClass("green").addClass("grey"); $("span", button).text("Aanpassen"); posting.fadeTo("slow", 0.5).fadeTo("fast", 1); }); return false; // dont propgate the event } // cancel active topic preview contained in specified form function cancel_topic_preview(form) { var existing = $("div.posting.preview", form); var button = $("a.preview_topic_link", form); var inputs = $("div.community_topic_form_inputs", form); if(!(existing.length)) return true; existing.remove(); inputs.fadeIn("fast"); button.removeClass("grey").addClass("green"); $("span", button).text("Preview"); return true; } // toggle preview/edit mode in preview-topic click handler function click_preview_topic_link(event) { event.preventDefault(); var form = $(this).closest("form"); var existing = $("div.posting.preview", form); var button = $("a.preview_topic_link", form); var inputs = $("div.community_topic_form_inputs", form); var title = $("input[name = 'title']").val(); var markup = $("textarea[name = 'content']").val(); // switch-back to edit-mode when existing preview found if(existing.length) { cancel_topic_preview(form); return false; } // fetch the preview for the markup currently in the textarea form_reset_messages(form); $.post("/preview", { token: mytoken, markup: markup, title: title, mode: "community" }, function(response) { // update client-side view on success var data = extract_preview_response(response); if(data.failure) { form_show_messages(form, data.failure); return false; } var posting = data.posting; posting.addClass("preview"); // remove the posting_header for the returend content (confusing) $("div.posting_header", posting).remove(); // insert posting before form-inputs and hide the form inputs posting.insertBefore(inputs); inputs.hide(); button.removeClass("green").addClass("grey"); $("span", button).text("Aanpassen"); posting.fadeTo("slow", 0.5).fadeTo("fast", 1); }); return false; // dont propgate the event } // fetch raw markup for specifeid posting and insert into reply textarea function click_quote_posting_link(event) { event.preventDefault(); var form = $("form.reply_form"); var textarea = $("textarea[name = 'content']", form); if(!(textarea.length)) return false; // resolve the posting using closest element match var posting = $(this).closest("div.posting"); if(!(posting.length)) return false; var matches = posting.attr("id").match(/^posting([0-9]+)$/); if(!(matches.length)) return false; var postingid = $.xn(matches[1]); // fetch the contents (eg: including markup) for this posting $.postJSON("/json/quotePosting", { postingid: postingid, token: mytoken }, function(data) { // update client-side view on success if(data.failure) { return false; } textarea.selection("insert", { text: "[quote]" + "[i]Door " + data.posting.author + ":[/i] " + data.posting.content + "[/quote]", mode: "before" }); // cancel any active reply and center window on textarea cancel_reply_preview(form); var newtop = textarea.offset().top - ( $(window).height() / 2); $.scrollTo({top: newtop}, 200, { axis: "y" }); }); return false; // dont propagate the event } // fetch raw markup for closest posting and invoke update editor function click_update_posting_link(event) { event.preventDefault(); // resolve the closest posting and posting body var posting = $(this).closest("div.posting"); if(!(posting.length)) return false; if($("div.posting_editor", posting).length) return false; var posting_body = $("div.posting_body", posting); if(!(posting_body.length)) return false; var matches = posting.attr("id").match(/^posting([0-9]+)$/); if(!(matches.length)) return false; var postingid = $.xn(matches[1]); var show_title = posting.hasClass("community") ? true : false; // fetch the contents (eg: including markup) for this content $.postJSON("/json/startUpdate", { postingid: postingid, token: mytoken }, function(data) { // invoke editor with initial value on success if(data.failure) { var failure = $("
").addClass("posting_body_failure").text(data.failure); var body_container = $("div.posting_body_container", posting); $("div.posting_body_failure", body_container).remove(); failure.prependTo(body_container); return false; // finished processing } posting_body.posting_editor({ initialValue: data.posting.content, initialTitle: data.posting.title, showTitle: show_title, onPreview: editor_preview_callback, onSubmit: editor_save_callback}) }); return false; // dont propgate the event } // posting editor callback for previewing updated conetnts function editor_preview_callback(editor) { var form = $(this).children("form"); var existing = $("div#update_preview", form); var editor_inputs = $("div.posting_editor_inputs", form); var button = $("a.posting_editor_preview", form); var posting = form.closest("div.posting"); var preview_mode = posting.hasClass("community") ? "community" : "reply"; // switch-back to edit-mode when existing preview found if(existing.length) { existing.remove(); editor_inputs.fadeIn("fast"); button.removeClass("grey").addClass("green"); $("span", button).text("Preview"); return true; } // fetch the preview for the markup currently in the editor form_reset_messages(form); $.post("/preview", { token: mytoken, markup: editor.getEditorValue(), title: editor.getTitleValue(), mode: preview_mode }, function(response) { // update client-side view on success var data = extract_preview_response(response); if(data.failure) { form_show_messages(form, data.failure); return false; } var posting = data.posting; posting.addClass("preview"); posting.attr("id", "update_preview"); // remove the posting_header for the returend content (confusing) $("div.posting_header", posting).remove(); // insert posting before inputs and hide editor inputs posting.insertBefore(editor_inputs); editor_inputs.hide(); button.removeClass("green").addClass("grey"); $("span", button).text("Aanpassen"); posting.fadeTo("slow", 0.5).fadeTo("fast", 1); }); return false; // dont propgate the event } // posting editor callback for saving updated contents function editor_save_callback(editor) { var form = $(this).children("form"); // resolve the closest posting and extract postingid var posting = $(this).closest("div.posting"); if(!(posting.length)) return false; var matches = posting.attr("id").match(/^posting([0-9]+)$/); if(!(matches.length)) return false; var postingid = $.xn(matches[1]); // attempt (async) updating the posting using json interface form_reset_messages(form); $.postJSON("/json/updatePosting", { postingid: postingid, content: editor.getEditorValue(), title: editor.getTitleValue(), token: mytoken }, function(data) { // update client-side view on success if(data.failure) { form_show_messages(form, data.failure); return false; } editor.closeEditor(); window.location.reload(); }) .fail(function() { form_show_messages(form, ["Opslaan mislukt"]); }); return true; } // submit the answer-form when single answer clicked function click_poll_answer(event) { event.preventDefault(); var form = $(this).closest("form.poll_answer_form"); return form_process_submit(form, poll_form_callback); } // update poll-posting based on the answer-form result function poll_form_callback(data, jqform, options) { var form = options.form_entry; if(data.failure) { form_show_messages(form, data.failure); return false; } if(!(data.poll)) return false; // concatenate markup for the new poll-answer entires var new_answers = ""; $.each(data.poll.answers, function(index, answer) { width = (answer.percentage * 165/100) + 1; new_answers += '