//global load for forms /* jqForm = Jquery object of the container that the form is being loaded into jqTable = Jquery object of the container where the item table resides data = JSON object of data being passed into the form urlLoad = The url that points to the AJAX form-loading process urlSave = The url that points to the AJAX form-saving process urlReload = The url that points to the AJAX table-reloading process reloadData = JSON object of data to pass to the reload MVC action (optional) onFinish = additional function(s) to call after the form loads (optional) onSave = additional function(s) to call after the form has been saved (optional) */ function LoadForm(jqForm, jqContainer, data, urlLoad, urlSave, urlReload, reloadData, onFinish, onSave, onReload) { var lang = { form_loading: "Loading Form" || "Loading Form", form_close: "Close" || "Close", form_cancel: "Cancel" || "Cancel" }; // Automatically add load message when colorbox opens AddLoadingMessage(jqForm, lang.form_loading); // Now load content into colorbox $(jqForm).load(urlLoad, data, function () { var container = $(this).children("div.portal-popup-container"); $(container).fadeIn(function () { $.colorbox.resize(MobileColorbox()); }); var footer = $(this).find("footer.portal-popup-footer"); var form = $(this).find("form.portal-popup-form"); $(form).trigger("reset"); // Reset the form in case it's being reloaded var saveButton = $(footer).find("a.button_ok"); if (!urlSave) $(footer).find("a.button_ok").addClass("hide"); var cancelButton = $(footer).find("a.button_cancel"); var id = parseInt($(jqForm).find("input.hidden_id").val()); if (id > 0) $(cancelButton).html(lang.form_close); else $(cancelButton).html(lang.form_cancel); // wire the add/save button if (urlSave) { $(saveButton).click(function () { PopupLoadingMessage(this); return SaveItem(form, jqForm, jqContainer, urlSave, urlLoad, urlReload, reloadData, onSave, onReload); }); } //Be sure that dynamic forms get re-parsed by the validation scripts $.validator.unobtrusive.parse(jqForm); //Leftover validation marks? Eliminate them $(this).find("input").removeClass("valid invalid"); //Something extra that needs to be done with the form? if (typeof onFinish === "function") { onFinish(); } }); } /* form = Form object being passed through jqObj = Jquery object of the container that the form is being loaded into jqTable = Jquery object of the container where the item table resides urlSave = The url that points to the AJAX saving process urlLoad = The url that points to the AJAX reload process urlReload = The url that points to the AJAX table-reloading process reloadData = JSON object of data to pass to the reload MVC action (optional) onSave = additional function(s) to call after the form has been saved (optional) */ // Save form item function SaveItem(form, jqForm, jqContainer, urlSave, urlLoad, urlReload, reloadData, onSave, onReload) { // get the button and message objects var buttons = $(jqForm).find("div.portal-popup-footer-buttons"); var status = $(jqForm).find("div.portal-popup-footer-status"); var message = $(jqForm).find("div.portal-popup-footer-message"); var id = parseInt($(jqForm).find("input.hidden_id").val()); // clear any messages out $(message).empty(); // save user $.ajax({ type: $(form).attr("method"), url: urlSave, data: $(form).serialize(), success: function (returndata) { if (returndata.ok) { // show confirmation and reload the related table if (id > 0) { var saved_copy = "Your changes have been saved." || "Your changes have been saved."; $(message).html("

" + saved_copy + "

"); SwitchElementViews(message, null); SwitchElementViews(buttons, status); } else ShowConfirmation(jqForm, form); if (urlReload) ReloadTable(jqContainer, urlReload, reloadData, onReload); //Something extra that needs to be done after the save? if (typeof onSave === "function") onSave(returndata); } else { var isValid = $(form).valid(); $(".input-validation-error").closest("div.form_item").addClass("form_item_error"); $(".valid").closest("div.form_item").removeClass("form_item_error"); $(message).html("

" + (returndata.message === null ? "Validation error. Please see error logs for more details." : returndata.message) + "

"); SwitchElementViews(message, null); SwitchElementViews(buttons, status); } }, error: function (data, errorThrown) { $(message).html("

Server Error: " + errorThrown + "

"); SwitchElementViews(message, null); SwitchElementViews(buttons, status); } }); return false; } /* btn = Button that handles the delete event jqForm = Jquery object of the container that the form is being loaded into urlConfirmation = The url that points to the AJAX deleting process callback = function that is called after action in popup data = parameters passed into the confirmation method */ //Delete form item function ConfirmationItem(btn, container, urlConfirmation, callback, data) { PopupLoadingMessage(btn); if (data) { $(container).load(urlConfirmation, data, function () { // if we have a callback function if (callback) { $.colorbox( { onClosed: function () { callback(); $(document).unbind('cbox_closed'); } }); } $.colorbox.close(); }); } else { $(container).load(urlConfirmation, function () { // if we have a callback function if (callback) { $.colorbox( { onClosed: function () { callback(); $(document).unbind('cbox_closed'); } }); } $.colorbox.close(); }); } } /* btn = Button that handles the delete event jqForm = Jquery object of the container that the form is being loaded into urlDelete = The url that points to the AJAX deleting process callback = function that is called after action in popup data = parameters passed into the confirmation method */ //Delete form item function DeleteItem(btn, jqForm, urlDelete, callback, data) { PopupLoadingMessage(btn); if (data) { $(jqForm).load(urlDelete, data, function () { // if we have a callback function if (callback) { $.colorbox( { onClosed: function () { callback(); $(document).unbind('cbox_closed'); } }); } $.colorbox.close(); }); } else { $(jqForm).load(urlDelete, function () { // if we have a callback function if (callback) { $.colorbox( { onClosed: function () { callback(); $(document).unbind('cbox_closed'); } }); } $.colorbox.close(); }); } } //This method is ONLY for deleting items out the database and NOT RELOADING CONTENT //It is assumed that the item has already been removed visually when this call is made function DeleteItemNoReload(btn, urlDelete, callback, data) { //PopupLoadingMessage(btn); if (callback) { $.colorbox({ onClosed: function () { callback(); $(document).unbind('cbox_closed'); } }); } $.colorbox.close(); if (data) $.post(urlDelete, data); else $.post(urlDelete); } //Switch to success message function ShowConfirmation(jqObj, form) { var container = jqObj.children(".portal-popup-container"); var confirmation = jqObj.children(".portal-popup-confirmation"); $(container).fadeOut(250, function () { $(form).trigger("reset"); var $fields = $(form).find("input, select"); $fields.removeClass("valid input-validation-error"); $(confirmation).fadeIn(250, function () { $.colorbox.resize(MobileColorbox()); }); }); } //Restore form from success message function RestoreForm(jqObj, restorecallback) { var parent = $(jqObj).closest("div.portal-popup-parent-container"); var container = parent.children(".portal-popup-container"); var confirmation = parent.children(".portal-popup-confirmation"); var form = parent.find("form.portal-popup-form"); $(confirmation).fadeOut(250, function () { var saveButton = parent.find("a.button_ok"); PopupLoadingMessage(saveButton, true); $(container).fadeIn(250, function () { $.colorbox.resize(MobileColorbox()); if (restorecallback) restorecallback(); }); }); } //Reload the table that's connected to the form function ReloadTable(jqObj, urlReload, data, onReload) { if (data) $(jqObj).load(urlReload, data, function () { //Something extra that needs to be done? if (typeof onReload === "function") { onReload(); } }); else $(jqObj).load(urlReload, function () { //Something extra that needs to be done? if (typeof onReload === "function") { onReload(); } }); } function ShowMoreItems(jqObj) { var showmore = $(jqObj).parent("td"); var sm_btn = $(jqObj); var sm_load = $(showmore).find("span.showmore"); SwitchElementViews(sm_load, sm_btn, false); var table = $(jqObj).closest("table.portal-table"); }