String.prototype.ucfirst = function() { return this.charAt(0).toUpperCase() + this.slice(1); } function deleteObject(type, action, id, event) { var destination = 'attributes'; var alternateDestinations = ['shadow_attributes', 'template_elements', 'taxonomies', 'objects', 'object_references']; if (alternateDestinations.indexOf(type) > -1) destination = type; else destination = type; url = "/" + destination + "/" + action + "/" + id; $.get(url, function(data) { openPopup("#confirmation_box"); $("#confirmation_box").html(data); }); } function quickDeleteSighting(id, rawId, context) { url = "/sightings/quickDelete/" + id + "/" + rawId + "/" + context; $.get(url, function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function publishPopup(id, type) { var action = "alert"; if (type == "publish") action = "publish"; var destination = 'attributes'; $.get( "/events/" + action + "/" + id, function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function delegatePopup(id) { simplePopup("/event_delegations/delegateEvent/" + id); } function genericPopup(url, popupTarget, callback) { $.get(url, function(data) { $(popupTarget).html(data); $(popupTarget).fadeIn(); left = ($(window).width() / 2) - ($(popupTarget).width() / 2); $(popupTarget).css({'left': left + 'px'}); $("#gray_out").fadeIn(); if (callback !== undefined) { callback(); } }); } function screenshotPopup(screenshotData, title) { popupHtml = ''; popupHtml += '
'; $('#screenshot_box').html(popupHtml); $('#screenshot_box').show(); left = ($(window).width() / 2) - ($('#screenshot-image').width() / 2); $('#screenshot_box').css({'left': left + 'px'}); $("#gray_out").fadeIn(); } function submitPublish(id, type) { $("#PromptForm").submit(); } function editTemplateElement(type, id) { simplePopup("/template_elements/edit/" + type + "/" + id); } function cancelPrompt(isolated) { if (isolated == undefined) { $("#gray_out").fadeOut(); } $("#confirmation_box").fadeOut(); $("#confirmation_box").empty(); } function submitDeletion(context_id, action, type, id) { var context = 'event'; if (type == 'template_elements') context = 'template'; var formData = $('#PromptForm').serialize(); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: formData, success:function (data, textStatus) { updateIndex(context_id, context); handleGenericAjaxResponse(data); }, complete:function() { $(".loading").hide(); $("#confirmation_box").fadeOut(); $("#gray_out").fadeOut(); }, type:"post", cache: false, url:"/" + type + "/" + action + "/" + id, }); } function removeSighting(caller) { var id = $(caller).data('id'); var rawid = $(caller).data('rawid'); var context = $(caller).data('context'); if (context != 'attribute') { context = 'event'; } var formData = $('#PromptForm').serialize(); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: formData, success:function (data, textStatus) { handleGenericAjaxResponse(data); var org = "/" + $('#org_id').text(); updateIndex(id, 'event'); $.get( "/sightings/listSightings/" + rawid + "/" + context + org, function(data) { $("#sightingsData").html(data); }); }, complete:function() { $(".loading").hide(); $("#confirmation_box").fadeOut(); }, type:"post", cache: false, url:"/sightings/quickDelete/" + id + "/" + rawid + "/" + context, }); } function toggleSetting(e, setting, id) { e.preventDefault(); e.stopPropagation(); switch (setting) { case 'warninglist_enable': formID = '#WarninglistIndexForm'; dataDiv = '#WarninglistData'; replacementForm = '/warninglists/getToggleField/'; searchString = 'enabled'; break; case 'favourite_tag': formID = '#FavouriteTagIndexForm'; dataDiv = '#FavouriteTagData'; replacementForm = '/favourite_tags/getToggleField/'; searchString = 'Adding'; break; case 'activate_object_template': formID = '#ObjectTemplateIndexForm'; dataDiv = '#ObjectTemplateData'; replacementForm = '/ObjectTemplates/getToggleField/'; searchString = 'activated'; break; case 'noticelist_enable': formID = '#NoticelistIndexForm'; dataDiv = '#NoticelistData'; replacementForm = '/noticelists/getToggleField/'; searchString = 'enabled'; break; } $(dataDiv).val(id); var formData = $(formID).serialize(); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: formData, success:function (data, textStatus) { var result = data; if (result.success) { var setting = false; if (result.success.indexOf(searchString) > -1) setting = true; $('#checkBox_' + id).prop('checked', setting); } handleGenericAjaxResponse(data); }, complete:function() { $.get(replacementForm, function(data) { $('#hiddenFormDiv').html(data); }); $(".loading").hide(); $("#confirmation_box").fadeOut(); $("#gray_out").fadeOut(); }, error:function() { handleGenericAjaxResponse({'saved':false, 'errors':['Request failed due to an unexpected error.']}); }, type:"post", cache: false, url: $(formID).attr('action'), }); } function initiatePasswordReset(id) { $.get( "/users/initiatePasswordReset/" + id, function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function submitPasswordReset(id) { var formData = $('#PromptForm').serialize(); var url = "/users/initiatePasswordReset/" + id; $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: formData, success:function (data, textStatus) { handleGenericAjaxResponse(data); }, complete:function() { $(".loading").hide(); $("#confirmation_box").fadeOut(); $("#gray_out").fadeOut(); }, type:"post", cache: false, url:url, }); } function submitMessageForm(url, form, target) { if (!$('#PostMessage').val()) { showMessage("fail", "Cannot submit empty message."); } else { submitGenericForm(url, form, target); } } function submitGenericForm(url, form, target) { $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: $('#' + form).serialize(), success:function (data, textStatus) { $('#top').html(data); showMessage("success", "Message added."); }, complete:function() { $(".loading").hide(); }, type:"post", cache: false, url:url, }); } function acceptObject(type, id, event) { name = '#ShadowAttribute_' + id + '_accept'; var formData = $(name).serialize(); $.ajax({ data: formData, success:function (data, textStatus) { updateIndex(event, 'event'); eventUnpublish(); handleGenericAjaxResponse(data); }, type:"post", cache: false, url:"/shadow_attributes/accept/" + id, }); } function toggleCorrelation(id) { $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: $('#PromptForm').serialize(), success:function (data, textStatus) { handleGenericAjaxResponse(data); $("#correlation_toggle_" + id).prop('checked', !$("#correlation_toggle_" + id).is(':checked')); }, complete:function() { $(".loading").hide(); $("#confirmation_box").fadeOut(); $("#gray_out").fadeOut(); }, type:"post", cache: false, url:'/attributes/toggleCorrelation/' + id, }); } function eventUnpublish() { $('.publishButtons').show(); $('.exportButtons').hide(); $('.published').hide(); $('.notPublished').show(); } function updateIndex(id, context, newPage) { if (typeof newPage !== 'undefined') page = newPage; var url, div; if (context == 'event') { url = currentUri; div = "#attributes_div"; } if (context == 'template') { url = "/template_elements/index/" + id; div = "#templateElements"; } $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", cache: false, success:function (data, textStatus) { $(".loading").hide(); $(div).html(data); if (typeof genericPopupCallback !== "undefined") { genericPopupCallback("success"); } else { console.log("genericPopupCallback function not defined"); } }, url: url, }); } function updateAttributeFieldOnSuccess(name, type, id, field, event) { $.ajax({ beforeSend: function (XMLHttpRequest) { if (field != 'timestamp') { $(".loading").show(); } }, dataType:"html", cache: false, success:function (data, textStatus) { if (field != 'timestamp') { $(".loading").hide(); $(name + '_solid').html(data); $(name + '_placeholder').empty(); $(name + '_solid').show(); } else { $('#' + type + '_' + id + '_' + 'timestamp_solid').html(data); } }, url:"/attributes/fetchViewValue/" + id + "/" + field, }); } function activateField(type, id, field, event) { resetForms(); if (type == 'denyForm') return; var objectType = 'attributes'; if (type == 'ShadowAttribute') { objectType = 'shadow_attributes'; } var name = '#' + type + '_' + id + '_' + field; var container_name = '#Attribute_' + id + '_' + field; $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", cache: false, success:function (data, textStatus) { $(".loading").hide(); $(container_name + '_placeholder').html(data); postActivationScripts(name, type, id, field, event); }, url:"/" + objectType + "/fetchEditForm/" + id + "/" + field, }); } function submitQuickTag(form) { $('#' + form).submit(); } //if someone clicks an inactive field, replace it with the hidden form field. Also, focus it and bind a focusout event, so that it gets saved if the user clicks away. //If a user presses enter, submit the form function postActivationScripts(name, type, id, field, event) { $(name + '_field').focus(); inputFieldButtonActive(name + '_field'); if (field == 'value' || field == 'comment') { autoresize($(name + '_field')[0]); $(name + '_field').on('keyup', function () { autoresize(this); }); } $(name + '_form').submit(function(e){ e.preventDefault(); submitForm(type, id, field, event); return false; }); $(name + '_form').bind("focusout", function() { inputFieldButtonPassive(name + '_field'); }); $(name + '_form').bind("focusin", function(){ inputFieldButtonActive(name + '_field'); }); $(name + '_form').bind("keydown", function(e) { if (e.ctrlKey && (e.keyCode == 13 || e.keyCode == 10)) { submitForm(type, id, field, event); } }); $(name + '_field').closest('.inline-input-container').children('.inline-input-accept').bind('click', function() { submitForm(type, id, field, event); }); $(name + '_field').closest('.inline-input-container').children('.inline-input-decline').bind('click', function() { resetForms(); }); $(name + '_solid').hide(); } function addSighting(type, attribute_id, event_id, page) { $('#Sighting_' + attribute_id + '_type').val(type); $.ajax({ data: $('#Sighting_' + attribute_id).closest("form").serialize(), cache: false, success:function (data, textStatus) { handleGenericAjaxResponse(data); var result = data; if (result.saved == true) { $('.sightingsCounter').each(function( counter ) { $(this).html(parseInt($(this).html()) + 1); }); updateIndex(event_id, 'event'); } }, error:function() { showMessage('fail', 'Request failed for an unknown reason.'); updateIndex(context, 'event'); }, type:"post", url:"/sightings/add/" + attribute_id }); } function resetForms() { $('.inline-field-solid').show(); $('.inline-field-placeholder').empty(); } function inputFieldButtonActive(selector) { $(selector).closest('.inline-input-container').children('.inline-input-accept').removeClass('inline-input-passive').addClass('inline-input-active'); $(selector).closest('.inline-input-container').children('.inline-input-decline').removeClass('inline-input-passive').addClass('inline-input-active'); } function inputFieldButtonPassive(selector) { $(selector).closest('.inline-input-container').children('.inline-input-accept').addClass('inline-input-passive').removeClass('inline-input-active'); $(selector).closest('.inline-input-container').children('.inline-input-daecline').addClass('inline-input-passive').removeClass('inline-input-active'); } function autoresize(textarea) { textarea.style.height = '20px'; textarea.style.height = (textarea.scrollHeight) + 'px'; } // submit the form - this can be triggered by unfocusing the activated form field or by submitting the form (hitting enter) // after the form is submitted, intercept the response and act on it function submitForm(type, id, field, context) { var object_type = 'attributes'; var action = "editField"; var name = '#' + type + '_' + id + '_' + field; if (type == 'ShadowAttribute') { object_type = 'shadow_attributes'; } $.ajax({ data: $(name + '_field').closest("form").serialize(), cache: false, success:function (data, textStatus) { handleAjaxEditResponse(data, name, type, id, field, context); }, error:function() { showMessage('fail', 'Request failed for an unknown reason.'); updateIndex(context, 'event'); }, type:"post", url:"/" + object_type + "/" + action + "/" + id }); $(name + '_field').unbind("keyup"); $(name + '_form').unbind("focusout"); return false; }; function quickSubmitTagForm(event_id, tag_id) { $('#EventTag').val(tag_id); $.ajax({ data: $('#EventSelectTagForm').closest("form").serialize(), beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, success:function (data, textStatus) { loadEventTags(event_id); handleGenericAjaxResponse(data); }, error:function() { showMessage('fail', 'Could not add tag.'); loadEventTags(event_id); }, complete:function() { $("#popover_form").fadeOut(); $("#gray_out").fadeOut(); $(".loading").hide(); }, type:"post", url:"/events/addTag/" + event_id }); return false; } function quickSubmitAttributeTagForm(attribute_id, tag_id) { $('#AttributeTag').val(tag_id); if (attribute_id == 'selected') { $('#AttributeAttributeIds').val(getSelected()); } $.ajax({ data: $('#AttributeSelectTagForm').closest("form").serialize(), beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, success:function (data, textStatus) { if (attribute_id == 'selected') { updateIndex(0, 'event'); } else { loadAttributeTags(attribute_id); } handleGenericAjaxResponse(data); }, error:function() { showMessage('fail', 'Could not add tag.'); loadAttributeTags(attribute_id); }, complete:function() { $("#popover_form").fadeOut(); $("#gray_out").fadeOut(); $(".loading").hide(); }, type:"post", url:"/attributes/addTag/" + attribute_id }); return false; } function handleAjaxEditResponse(data, name, type, id, field, event) { responseArray = data; if (type == 'Attribute') { if (responseArray.saved) { showMessage('success', responseArray.success); updateAttributeFieldOnSuccess(name, type, id, field, event); updateAttributeFieldOnSuccess(name, type, id, 'timestamp', event); eventUnpublish(); } else { showMessage('fail', 'Validation failed: ' + responseArray.errors.value); updateAttributeFieldOnSuccess(name, type, id, field, event); } } if (type == 'ShadowAttribute') { updateIndex(event, 'event'); } if (responseArray.hasOwnProperty('check_publish')) { checkAndSetPublishedInfo(); } } function handleGenericAjaxResponse(data) { if (typeof data == 'string') { responseArray = JSON.parse(data); } else { responseArray = data; } if (responseArray.saved) { showMessage('success', responseArray.success); if (responseArray.hasOwnProperty('check_publish')) { checkAndSetPublishedInfo(); } return true; } else { showMessage('fail', responseArray.errors); return false; } } function toggleAllAttributeCheckboxes() { if ($(".select_all").is(":checked")) { $(".select_attribute").prop("checked", true); $(".select_proposal").prop("checked", true); } else { $(".select_attribute").prop("checked", false); $(".select_proposal").prop("checked", false); } } function toggleAllCheckboxes() { if ($(".select_all").is(":checked")) { $(".select").prop("checked", true); } else { $(".select").prop("checked", false); } } function toggleAllTaxonomyCheckboxes() { if ($(".select_all").is(":checked")) { $(".select_taxonomy").prop("checked", true); } else { $(".select_taxonomy").prop("checked", false); } } function attributeListAnyAttributeCheckBoxesChecked() { if ($('.select_attribute:checked').length > 0) $('.mass-select').removeClass('hidden'); else $('.mass-select').addClass('hidden'); } function listCheckboxesChecked() { if ($('.select:checked').length > 0) $('.mass-select').removeClass('hidden'); else $('.mass-select').addClass('hidden'); } function attributeListAnyProposalCheckBoxesChecked() { if ($('.select_proposal:checked').length > 0) $('.mass-proposal-select').removeClass('hidden'); else $('.mass-proposal-select').addClass('hidden'); } function taxonomyListAnyCheckBoxesChecked() { if ($('.select_taxonomy:checked').length > 0) $('.mass-select').show(); else $('.mass-select').hide(); } function multiSelectDeleteEvents() { var selected = []; $(".select").each(function() { if ($(this).is(":checked")) { var temp = $(this).data("id"); if (temp != null) { selected.push(temp); } } }); $.get("/events/delete/" + JSON.stringify(selected), function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function multiSelectToggleFeeds(on, cache) { var selected = []; $(".select").each(function() { if ($(this).is(":checked")) { var temp = $(this).data("id"); if (temp != null) { selected.push(temp); } } }); $.get("/feeds/toggleSelected/" + on + "/" + cache + "/" + JSON.stringify(selected), function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function multiSelectAction(event, context) { var settings = { deleteAttributes: { confirmation: "Are you sure you want to delete all selected attributes?", controller: "attributes", camelCase: "Attribute", alias: "attribute", action: "delete", }, acceptProposals: { confirmation: "Are you sure you want to accept all selected proposals?", controller: "shadow_attributes", camelCase: "ShadowAttribute", alias: "proposal", action: "accept", }, discardProposals: { confirmation: "Are you sure you want to discard all selected proposals?", controller: "shadow_attributes", camelCase: "ShadowAttribute", alias: "proposal", action: "discard", }, }; var answer = confirm("Are you sure you want to " + settings[context]["action"] + " all selected " + settings[context]["alias"] + "s?"); if (answer) { var selected = []; $(".select_" + settings[context]["alias"]).each(function() { if ($(this).is(":checked")) { var temp= $(this).data("id"); selected.push(temp); } }); $('#' + settings[context]["camelCase"] + 'Ids' + settings[context]["action"].ucfirst()).attr('value', JSON.stringify(selected)); var formData = $('#' + settings[context]["action"] + '_selected').serialize(); $.ajax({ data: formData, cache: false, type:"POST", url:"/" + settings[context]["controller"] + "/" + settings[context]["action"] + "Selected/" + event, success:function (data, textStatus) { updateIndex(event, 'event'); var result = handleGenericAjaxResponse(data); if (settings[context]["action"] != "discard" && result == true) eventUnpublish(); }, }); } return false; } function editSelectedAttributes(event) { simplePopup("/attributes/editSelected/" + event); } function addSelectedTaxonomies(taxonomy) { $.get("/taxonomies/taxonomyMassConfirmation/"+taxonomy, function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function submitMassTaxonomyTag() { $('#PromptForm').submit(); } function submitMassEventDelete() { $('#PromptForm').trigger('submit'); event.preventDefault(); } function getSelected() { var selected = []; $(".select_attribute").each(function() { if ($(this).is(":checked")) { var test = $(this).data("id"); selected.push(test); } }); return JSON.stringify(selected); } function getSelectedTaxonomyNames() { var selected = []; $(".select_taxonomy").each(function() { if ($(this).is(":checked")) { var row = $(this).data("id"); var temp = $('#tag_' + row).html(); temp = $("
").html(temp).text(); selected.push(temp); } }); $('#TaxonomyNameList').val(JSON.stringify(selected)); } function loadEventTags(id) { $.ajax({ dataType:"html", cache: false, success:function (data, textStatus) { $(".eventTagContainer").html(data); }, url:"/tags/showEventTag/" + id, }); } function removeEventTag(event, tag) { var answer = confirm("Are you sure you want to remove this tag from the event?"); if (answer) { var formData = $('#removeTag_' + tag).serialize(); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: formData, type:"POST", cache: false, url:"/events/removeTag/" + event + '/' + tag, success:function (data, textStatus) { loadEventTags(event); handleGenericAjaxResponse(data); }, complete:function() { $(".loading").hide(); } }); } return false; } function loadAttributeTags(id) { $.ajax({ dataType:"html", cache: false, success:function (data, textStatus) { $("#Attribute_"+id+"_tr .attributeTagContainer").html(data); }, url:"/tags/showAttributeTag/" + id }); } function removeObjectTagPopup(context, object, tag) { $.get( "/" + context + "s/removeTag/" + object + '/' + tag, function(data) { $("#confirmation_box").html(data); openPopup("#confirmation_box"); }); } function removeObjectTag(context, object, tag) { var formData = $('#PromptForm').serialize(); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, data: formData, type:"POST", cache: false, url:"/" + context.toLowerCase() + "s/removeTag/" + object + '/' + tag, success:function (data, textStatus) { $("#confirmation_box").fadeOut(); $("#gray_out").fadeOut(); if (context == 'Attribute') { loadAttributeTags(object); } else { loadEventTags(object); } handleGenericAjaxResponse(data); }, complete:function() { $(".loading").hide(); } }); return false; } function clickCreateButton(event, type) { var destination = 'attributes'; if (type == 'Proposal') destination = 'shadow_attributes'; simplePopup("/" + destination + "/add/" + event); } function submitPopoverForm(context_id, referer, update_context_id) { var url = null; var context = 'event'; var contextNamingConvention = 'Attribute'; var closePopover = true; switch (referer) { case 'add': url = "/attributes/add/" + context_id; break; case 'edit': url = "/attributes/edit/" + context_id; break; case 'propose': url = "/shadow_attributes/add/" + context_id; break; case 'massEdit': url = "/attributes/editSelected/" + context_id; break; case 'addTextElement': url = "/templateElements/add/text/" + context_id; context = 'template'; contextNamingConvention = 'TemplateElementText'; break; case 'editTextElement': url = "/templateElements/edit/text/" + context_id; context = 'template'; context_id = update_context_id; contextNamingConvention = 'TemplateElementText'; break; case 'addAttributeElement': url = "/templateElements/add/attribute/" + context_id; context = 'template'; contextNamingConvention = 'TemplateElementAttribute'; break; case 'editAttributeElement': url = "/templateElements/edit/attribute/" + context_id; context = 'template'; context_id = update_context_id; contextNamingConvention = 'TemplateElementAttribute'; break; case 'addFileElement': url = "/templateElements/add/file/" + context_id; context = 'template'; contextNamingConvention = 'TemplateElementFile'; break; case 'editFileElement': url = "/templateElements/edit/file/" + context_id; context = 'template'; context_id = update_context_id; contextNamingConvention = 'TemplateElementFile'; break; case 'replaceAttributes': url = "/attributes/attributeReplace/" + context_id; break; case 'addSighting': url = "/sightings/add/" + context_id; closePopover = false; break; case 'addObjectReference': url = "/objectReferences/add/" + context_id; break; } if (url !== null) { $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); if (closePopover) { $("#gray_out").fadeOut(); $("#popover_form").fadeOut(); } }, data: $("#submitButton").closest("form").serialize(), success:function (data, textStatus) { var result; if (closePopover) { result = handleAjaxPopoverResponse(data, context_id, url, referer, context, contextNamingConvention); } if (referer == 'addSighting') { updateIndex(update_context_id, 'event'); $.get( "/sightings/listSightings/" + id + "/attribute", function(data) { $("#sightingsData").html(data); }); $('.sightingsToggle').removeClass('btn-primary'); $('.sightingsToggle').addClass('btn-inverse'); $('#sightingsListAllToggle').removeClass('btn-inverse'); $('#sightingsListAllToggle').addClass('btn-primary'); } if (context == 'event' && (referer == 'add' || referer == 'massEdit' || referer == 'replaceAttributes' || referer == 'addObjectReference')) eventUnpublish(); $(".loading").hide(); }, type:"post", url:url }); } return false; }; function handleAjaxPopoverResponse(response, context_id, url, referer, context, contextNamingConvention) { responseArray = response; var message = null; var result = "fail"; if (responseArray.saved) { updateIndex(context_id, context); if (responseArray.success) { showMessage("success", responseArray.success); result = "success"; } if (responseArray.errors) { showMessage("fail", responseArray.errors); } } else { var savedArray = saveValuesForPersistance(); $.ajax({ async:true, dataType:"html", success:function (data, textStatus) { $("#popover_form").html(data); openPopup("#popover_form"); var error_context = context.charAt(0).toUpperCase() + context.slice(1); handleValidationErrors(responseArray.errors, context, contextNamingConvention); result = "success"; if (!$.isEmptyObject(responseArray)) { result = "fail"; $("#formWarning").show(); $("#formWarning").html('The object(s) could not be saved. Please, try again.'); } recoverValuesFromPersistance(savedArray); $(".loading").hide(); }, url:url }); } return result; } //before we update the form (in case the action failed), we want to retrieve the data from every field, so that we can set the fields in the new form that we fetch function saveValuesForPersistance() { var formPersistanceArray = new Array(); for (i = 0; i < fieldsArray.length; i++) { formPersistanceArray[fieldsArray[i]] = $('#' + fieldsArray[i]).val(); } return formPersistanceArray; } function recoverValuesFromPersistance(formPersistanceArray) { for (i = 0; i < fieldsArray.length; i++) { $('#' + fieldsArray[i]).val(formPersistanceArray[fieldsArray[i]]); } } function handleValidationErrors(responseArray, context, contextNamingConvention) { for (var k in responseArray) { var elementName = k.charAt(0).toUpperCase() + k.slice(1); $("#" + contextNamingConvention + elementName).parent().addClass("error"); $("#" + contextNamingConvention + elementName).parent().append("
" + responseArray[k] + "
"); } } function toggleHistogramType(type, old) { var done = false; old.forEach(function(entry) { if (type == entry) { done = true; old.splice(old.indexOf(entry), 1); } }); if (done == false) old.push(type); updateHistogram(JSON.stringify(old)); } function updateHistogram(selected) { $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", cache: false, success:function (data, textStatus) { $(".loading").hide(); $("#histogram").html(data); }, url:"/users/histogram/" + selected, }); } function showMessage(success, message, context) { if (typeof context !== "undefined") { $("#ajax_" + success, window.parent.document).html(message); var duration = 1000 + (message.length * 40); $("#ajax_" + success + "_container", window.parent.document).fadeIn("slow"); $("#ajax_" + success + "_container", window.parent.document).delay(duration).fadeOut("slow"); } $("#ajax_" + success).html(message); var duration = 1000 + (message.length * 40); $("#ajax_" + success + "_container").fadeIn("slow"); $("#ajax_" + success + "_container").delay(duration).fadeOut("slow"); } function cancelPopoverForm() { $("#gray_out").fadeOut(); $("#popover_form").fadeOut(); $("#screenshot_box").fadeOut(); $("#confirmation_box").fadeOut(); $('#gray_out').fadeOut(); $('#popover_form').fadeOut(); } function activateTagField() { $("#addTagButton").hide(); $("#addTagField").show(); } function tagFieldChange() { if ($("#addTagField :selected").val() > 0) { var selected_id = $("#addTagField :selected").val(); var selected_text = $("#addTagField :selected").text(); if ($.inArray(selected_id, selectedTags)==-1) { selectedTags.push(selected_id); appendTemplateTag(selected_id); } } $("#addTagButton").show(); $("#addTagField").hide(); } function appendTemplateTag(selected_id) { $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", cache: false, success:function (data, textStatus) { $(".loading").hide(); $("#tags").append(data); }, url:"/tags/viewTag/" + selected_id, }); updateSelectedTags(); } function addAllTags(tagArray) { parsedTagArray = JSON.parse(tagArray); parsedTagArray.forEach(function(tag) { appendTemplateTag(tag); }); } function removeTemplateTag(id, name) { selectedTags.forEach(function(tag) { if (tag == id) { var index = selectedTags.indexOf(id); if (index > -1) { selectedTags.splice(index, 1); updateSelectedTags(); } } }); $('#tag_bubble_' + id).remove(); } function updateSelectedTags() { $('#hiddenTags').attr("value", JSON.stringify(selectedTags)); } function saveElementSorting(order) { $.ajax({ data: order, dataType:"json", contentType: "application/json", cache: false, success:function (data, textStatus) { handleGenericAjaxResponse(data); }, type:"post", cache: false, url:"/templates/saveElementSorting/", }); } function templateAddElementClicked(id) { simplePopup("/template_elements/templateElementAddChoices/" + id); } function templateAddElement(type, id) { simplePopup("/template_elements/add/" + type + "/" + id); } function templateUpdateAvailableTypes() { $("#innerTypes").empty(); var type = $("#TemplateElementAttributeType option:selected").text(); var complex = $('#TemplateElementAttributeComplex:checked').val(); if (complex && type != 'Select Type') { currentTypes.forEach(function(entry) { $("#innerTypes").append("
" + entry + "
"); }); $('#outerTypes').show(); } else $('#outerTypes').hide(); } function populateTemplateTypeDropdown() { var cat = $("#TemplateElementAttributeCategory option:selected").text(); currentTypes = []; if (cat == 'Select Category') { $('#TemplateElementAttributeType').html(""); } else { var complex = $('#TemplateElementAttributeComplex:checked').val(); if (cat in typeGroupCategoryMapping) { $('#TemplateElementAttributeType').html(""); typeGroupCategoryMapping[cat].forEach(function(entry) { $('#TemplateElementAttributeType').append(""); }); } else { complex = false; } if (!complex) { $('#TemplateElementAttributeType').html(""); categoryTypes[cat].forEach(function(entry) { $('#TemplateElementAttributeType').append(""); }); } } } function templateElementAttributeTypeChange() { var complex = $('#TemplateElementAttributeComplex:checked').val(); var type = $("#TemplateElementAttributeType option:selected").text(); currentTypes = []; if (type != 'Select Type') { if (complex) { complexTypes[type]["types"].forEach(function(entry) { currentTypes.push(entry); }); } else { currentTypes.push(type); } } else { currentTypes = []; } $("#typeJSON").html(JSON.stringify(currentTypes)); templateUpdateAvailableTypes(); } function templateElementAttributeCategoryChange(category) { if (category in typeGroupCategoryMapping) { $('#complexToggle').show(); } else { $('#complexToggle').hide(); } if (category != 'Select Type') { populateTemplateTypeDropdown(); } templateUpdateAvailableTypes(); } function templateElementFileCategoryChange(category) { if (category == '') { $("#TemplateElementFileMalware")[0].disabled = true; $("#TemplateElementFileMalware")[0].checked = false; } else { if (categoryArray[category].length == 2) { $("#TemplateElementFileMalware")[0].disabled = false; $("#TemplateElementFileMalware")[0].checked = true; } else { $("#TemplateElementFileMalware")[0].disabled = true; if (categoryArray[category] == 'attachment') $("#TemplateElementFileMalware")[0].checked = false; else $("#TemplateElementFileMalware")[0].checked = true; } } } function openPopup(id) { var window_height = $(window).height(); var popup_height = $(id).height(); if (window_height < popup_height) { $(id).css("top", 0); $(id).css("height", window_height); $(id).addClass('vertical-scroll'); } else { if (window_height > (300 + popup_height)) { var top_offset = ((window_height - popup_height) / 2) - 150; } else { var top_offset = (window_height - popup_height) / 2; } $(id).css("top", top_offset + 'px'); } $("#gray_out").fadeIn(); $(id).fadeIn(); } function getPopup(id, context, target, admin, popupType) { $("#gray_out").fadeIn(); var url = ""; if (typeof admin !== 'undefined' && admin != '') url+= "/admin"; if (context != '') url += "/" + context; if (target != '') url += "/" + target; if (id != '') url += "/" + id; if (popupType == '' || typeof popupType == 'undefined') popupType = '#popover_form'; $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", async: true, cache: false, success:function (data, textStatus) { $(".loading").hide(); $(popupType).html(data); openPopup(popupType); }, error:function() { $(".loading").hide(); $("#gray_out").fadeOut(); showMessage('fail', 'Something went wrong - the queried function returned an exception. Contact your administrator for further details (the exception has been logged).'); }, url: url }); } function simplePopup(url) { $("#gray_out").fadeIn(); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", async: true, cache: false, success:function (data, textStatus) { $(".loading").hide(); $("#popover_form").html(data); openPopup("#popover_form"); }, url: url, }); } function choicePopup(legend, list) { var popupHtml = '
'; popupHtml += 'Select Object Category'; popupHtml += '
'; popupHtml += ''; popupHtml += ''; for (var item of list) { popupHtml += ''; popupHtml += ''; popupHtml += ''; } popupHtml += ''; popupHtml += '
'+item.text+'
'; popupHtml += '
'; popupHtml += '
Cancel
'; popupHtml += '
'; $("#popover_form").html(popupHtml); openPopup("#popover_form"); } function resizePopoverBody() { var bodyheight = $(window).height(); bodyheight = 3 * bodyheight / 4 - 150; $("#popover_choice_main").css({"max-height": bodyheight}); } function populateTemplateHiddenFileDiv(files) { $('#TemplateFileArray').val(JSON.stringify(files)); } function populateTemplateFileBubbles() { var fileObjectArray = JSON.parse($('#TemplateFileArray').val()); fileObjectArray.forEach(function(entry) { templateAddFileBubble(entry.element_id, false, entry.filename, entry.tmp_name, 'yes'); }); } function templateFileHiddenAdd(files, element_id, batch) { var fileArray = $.parseJSON($('#TemplateFileArray', window.parent.document).val()); var contained = false; for (var j=0; j< files.length; j++) { for (var i=0; i< fileArray.length; i++) { if (fileArray[i].filename == files[j].filename) { contained = true; } if (batch == 'no' && fileArray[i].element_id == element_id) { templateDeleteFileBubble(fileArray[i].filename, fileArray[i].tmp_name, fileArray[i].element_id, 'iframe', batch); contained = false; var removeId = i; } } if (batch == 'no') fileArray.splice(removeId, 1); if (contained == false) { fileArray.push(files[j]); templateAddFileBubble(element_id, true, files[j].filename, files[j].tmp_name, batch); $('#TemplateFileArray', window.parent.document).val(JSON.stringify(fileArray)); } } } function htmlEncode(value){ return $('
').text(value).html(); } function templateAddFileBubble(element_id, iframe, filename, tmp_name, batch) { filename = htmlEncode(filename); tmp_name = htmlEncode(tmp_name); if (batch == 'no') { if (iframe == true) { $('#filenames_' + element_id, window.parent.document).html('
' + filename + 'x
'); } else { $('#filenames_' + element_id).html('
' + filename + 'x
'); } } else { if (iframe == true) { $('#filenames_' + element_id, window.parent.document).append('
' + filename + 'x
'); } else { $('#filenames_' + element_id).append('
' + filename + 'x
'); } } } function templateDeleteFileBubble(filename, tmp_name, element_id, context, batch) { $(".loading").show(); $.ajax({ type:"post", cache: false, url:"/templates/deleteTemporaryFile/" + tmp_name, }); var c = this; if (context == 'iframe') { $('#' + tmp_name + '_container', window.parent.document).remove(); var oldArray = JSON.parse($('#TemplateFileArray', window.parent.document).val()); } else { $('#' + tmp_name + '_container').remove(); var oldArray = JSON.parse($('#TemplateFileArray').val()); } var newArray = []; oldArray.forEach(function(entry) { if (batch == 'no') { if (entry.element_id != element_id) { newArray.push(entry); } } else { if (entry.tmp_name != tmp_name) { newArray.push(entry); } } }); if (batch == 'no') { $('#fileUploadButton_' + element_id, $('#iframe_' + element_id).contents()).html('Upload File'); } if (context == 'iframe') { $('#TemplateFileArray', window.parent.document).val(JSON.stringify(newArray)); } else { $('#TemplateFileArray').val(JSON.stringify(newArray)); } $(".loading").hide(); } function templateFileUploadTriggerBrowse(id) { $('#upload_' + id + '_file').click(); } function freetextRemoveRow(id, event_id) { $('#row_' + id).hide(); $('#Attribute' + id + 'Save').attr("value", "0"); if ($(".freetext_row:visible").length == 0) { window.location = "/events/" + event_id; } } function indexEvaluateFiltering() { if (filterContext == "event") { if (filtering.published != 2) { $('#value_published').html(publishedOptions[filtering.published]); } else { $('#value_published').html(""); } if (filtering.hasproposal != 2) { $('#value_hasproposal').html(publishedOptions[filtering.hasproposal]); } else { $('#value_hasproposal').html(""); } if (filtering.date.from != null || filtering.date.from != null) { var text = ""; if (filtering.date.from != "") text = "From: " + $('').text(filtering.date.from).html(); if (filtering.date.until != "") { if (text != "") text += " "; text += "Until: " + $('').text(filtering.date.until).html(); } } $('#value_date').html(text); for (var i = 0; i < simpleFilters.length; i++) { indexEvaluateSimpleFiltering(simpleFilters[i]); } indexRuleChange(); } else { for (var i = 0; i < differentFilters.length; i++) { if (filtering[differentFilters[i]] != "") { var text = ""; if (filtering[differentFilters[i]] == 1) text = "Yes"; else if (filtering[differentFilters[i]] == 0) text = "No"; $('#value_' + differentFilters[i]).text(text); } else { $('#value_' + differentFilters[i]).text(""); } } for (var i = 0; i < simpleFilters.length; i++) { indexEvaluateSimpleFiltering(simpleFilters[i]); } } indexSetTableVisibility(); indexSetRowVisibility(); $('#generatedURLContent').text(indexCreateFilters()); } function quickFilter(passedArgs, url) { if(!passedArgs){ var passedArgs = []; } if( $('#quickFilterField').val().trim().length > 0){ passedArgs["searchall"] = $('#quickFilterField').val().trim(); for (var key in passedArgs) { if (key !== 'page') { url += "/" + key + ":" + passedArgs[key]; } } } window.location.href=url; } function executeFilter(passedArgs, url) { for (var key in passedArgs) url += "/" + key + ":" + passedArgs[key]; window.location.href=url; } function quickFilterTaxonomy(taxonomy_id, passedArgs) { var url = "/taxonomies/view/" + taxonomy_id + "/filter:" + $('#quickFilterField').val(); window.location.href=url; } function quickFilterRemoteEvents(passedArgs, id) { passedArgs["searchall"] = $('#quickFilterField').val(); var url = "/servers/previewIndex/" + id; for (var key in passedArgs) { url += "/" + key + ":" + passedArgs[key]; } window.location.href=url; } $('#quickFilterField').bind("enterKey",function(e){ $('#quickFilterButton').trigger("click"); }); $('#quickFilterField').keyup(function(e){ if(e.keyCode == 13) { $('#quickFilterButton').trigger("click"); } }); function remoteIndexApplyFilters() { var url = actionUrl + '/' + $("#EventFilter").val(); window.location.href = url; } function indexApplyFilters() { var url = indexCreateFilters(); window.location.href = url; } function indexCreateFilters() { text = ""; if (filterContext == 'event') { if (filtering.published != "2") { text += "searchpublished:" + filtering.published; } if (filtering.hasproposal != "2") { if (text != "") text += "/"; text += "searchhasproposal:" + filtering.hasproposal; } } else { for (var i = 0; i < differentFilters.length; i++) { if (filtering[differentFilters[i]]) { if (text != "") text += "/"; text += "search" + differentFilters[i] + ":" + filtering[differentFilters[i]]; } } } for (var i = 0; i < simpleFilters.length; i++) { text = indexBuildArray(simpleFilters[i], text); } if (filterContext == 'event') { if (filtering.date.from) { if (text != "") text += "/"; text += "searchDatefrom:" + filtering.date.from; } if (filtering.date.until) { if (text != "") text += "/"; text += "searchDateuntil:" + filtering.date.until; } return baseurl + '/events/index/' + text; } else { return baseurl + '/admin/users/index/' + text; } } function indexBuildArray(type, text) { temp = ""; if (text != "") temp += "/"; temp += "search" + type + ":"; if (filtering[type].NOT.length == 0 && filtering[type].OR.length == 0) return text; var swap = filtering[type].OR.length; var temp_array = filtering[type].OR.concat(filtering[type].NOT); for (var i = 0; i < temp_array.length; i++) { if (i > 0) temp += "|"; if (i >= swap) temp +="!"; temp += temp_array[i]; } text += temp; return text; } function indexSetRowVisibility() { for (var i = 0; i < allFields.length; i++) { if ($("#value_" + allFields[i]).text().trim() != "") { $("#row_" + allFields[i]).show(); } else { $("#row_" + allFields[i]).hide(); } } } function indexEvaluateSimpleFiltering(field) { text = ""; if (filtering[field].OR.length == 0 && filtering[field].NOT.length == 0) { $('#value_' + field).html(text); return false; } if (filtering[field].OR.length !=0) { for (var i = 0; i < filtering[field].OR.length; i++) { if (i > 0) text += ' OR '; if (typedFields.indexOf(field) == -1) { text += $('').text(filtering[field].OR[i]).html(); } else { for (var j = 0; j < typeArray[field].length; j++) { if (typeArray[field][j].id == filtering[field].OR[i]) { text += $('').text(typeArray[field][j].value).html(); } } } } } if (filtering[field].NOT.length !=0) { for (var i = 0; i < filtering[field].NOT.length; i++) { if (i == 0) { if (text != "") text += ' AND NOT '; else text += 'NOT '; } else text += ' AND NOT '; if (typedFields.indexOf(field) == -1) { text += $('').text(filtering[field].NOT[i]).html(); } else { for (var j = 0; j < typeArray[field].length; j++) { if (typeArray[field][j].id == filtering[field].NOT[i]) { text += $('').text(typeArray[field][j].value).html(); } } } } } $('#value_' + field).html(text); } function indexAddRule(param) { var found = false; if (filterContext == 'event') { if (param.data.param1 == "date") { var val1 = escape($('#EventSearch' + param.data.param1 + 'from').val()); var val2 = escape($('#EventSearch' + param.data.param1 + 'until').val()); if (val1 != "") filtering.date.from = val1; if (val2 != "") filtering.date.until = val2; } else if (param.data.param1 == "published") { var value = escape($('#EventSearchpublished').val()); if (value != "") filtering.published = value; } else if (param.data.param1 == "hasproposal") { var value = escape($('#EventSearchhasproposal').val()); if (value != "") filtering.hasproposal = value; } else { var value = escape($('#EventSearch' + param.data.param1).val()); var operator = operators[escape($('#EventSearchbool').val())]; if (value != "" && filtering[param.data.param1][operator].indexOf(value) < 0) filtering[param.data.param1][operator].push(value); } } else if (filterContext = 'user') { if (differentFilters.indexOf(param.data.param1) != -1) { var value = escape($('#UserSearch' + param.data.param1).val()); if (value != "") filtering[param.data.param1] = value; } else { var value = escape($('#UserSearch' + param.data.param1).val()); var operator = operators[escape($('#UserSearchbool').val())]; if (value != "" && filtering[param.data.param1][operator].indexOf(value) < 0) filtering[param.data.param1][operator].push(value); } } indexEvaluateFiltering(); } function indexSetTableVisibility() { var visible = false; if ($("[id^='value_']").text().trim()!="" && $("[id^='value_']").text().trim()!="-1") { visible = true; } if (visible == true) $('#FilterplaceholderTable').hide(); else $('#FilterplaceholderTable').show(); } function indexRuleChange() { var context = filterContext.charAt(0).toUpperCase() + filterContext.slice(1); $('[id^=' + context + 'Search]').hide(); var rule = $('#' + context + 'Rule').val(); var fieldName = '#' + context + 'Search' + rule; if (fieldName == '#' + context + 'Searchdate') { $(fieldName + 'from').show(); $(fieldName + 'until').show(); } else { $(fieldName).show(); } if (simpleFilters.indexOf(rule) != -1) { $('#' + context + 'Searchbool').show(); } else $('#' + context + 'Searchbool').hide(); $('#addRuleButton').show(); $('#addRuleButton').unbind("click"); $('#addRuleButton').click({param1: rule}, indexAddRule); } function indexFilterClearRow(field) { $('#value_' + field).html(""); $('#row_' + field).hide(); if (field == "date") { filtering.date.from = ""; filtering.date.until = ""; } else if (field == "published") { filtering.published = 2; } else if (field == "hasproposal") { filtering.hasproposal = 2; } else if (differentFilters.indexOf(field) != -1) { filtering[field] = ""; } else { filtering[field].NOT = []; filtering[field].OR = []; } indexSetTableVisibility(); indexEvaluateFiltering(); } function restrictEventViewPagination() { var showPages = new Array(); var start; var end; var i; if (page < 6) { start = 1; if (count - page < 6) { end = count; } else { end = page + (9 - (page - start)); } } else if (count - page < 6) { end = count; start = count - 10; } else { start = page-5; end = page+5; } if (start > 2) { $("#apage" + start).parent().before("
  • ...
  • "); $("#aExpandLeft").click(function() {expandPagination(0, 0); return false;}); $("#bpage" + start).parent().before("
  • ...
  • "); $("#bExpandLeft").click(function() {expandPagination(1, 0); return false;}) } if (end < (count - 1)) { $("#apage" + end).parent().after("
  • ...
  • "); $("#aExpandRight").click(function() {expandPagination(0, 1); return false;}); $("#bpage" + end).parent().after("
  • ...
  • "); $("#bExpandRight").click(function() {expandPagination(1, 1); return false;}) } for (i = 1; i < (count+1); i++) { if (i != 1 && i != count && (i < start || i > end)) { $("#apage" + i).hide(); $("#bpage" + i).hide(); } } } function expandPagination(bottom, right) { var i; var prefix = "a"; if (bottom == 1) prefix = "b"; var start = 1; var end = page; if (right == 1) { start = page; end = count; $("#" + prefix + "ExpandRight").remove(); } else $("#" + prefix + "ExpandLeft").remove(); for (i = start; i < end; i++) { $("#" + prefix + "page" + i).show(); } } function getSubGroupFromSetting(setting) { var temp = setting.split('.'); if (temp[0] == "Plugin") { temp = temp[1]; if (temp.indexOf('_') > -1) { temp = temp.split('_'); return temp[0]; } } return 'general'; } function serverSettingsActivateField(setting, id) { resetForms(); $('.inline-field-placeholder').hide(); var fieldName = "#setting_" + getSubGroupFromSetting(setting) + "_" + id; $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, dataType:"html", cache: false, success:function (data, textStatus) { $(".loading").hide(); $(fieldName + "_placeholder").html(data); $(fieldName + "_solid").hide(); $(fieldName + "_placeholder").show(); serverSettingsPostActivationScripts(fieldName, setting, id); }, url:"/servers/serverSettingsEdit/" + setting + "/" + id, }); } function serverSettingsPostActivationScripts(name, setting, id) { $(name + '_field').focus(); inputFieldButtonActive(name + '_field'); $(name + '_form').submit(function(e){ e.preventDefault(); serverSettingSubmitForm(name, setting, id); return false; }); $(name + '_form').bind("focusout", function() { inputFieldButtonPassive(name + '_field'); }); $(name + '_form').bind("focusin", function(){ inputFieldButtonActive(name + '_field'); }); $(name + '_form').bind("keydown", function(e) { if (e.ctrlKey && (e.keyCode == 13 || e.keyCode == 10)) { serverSettingSubmitForm(name, setting, id); } }); $(name + '_field').closest('.inline-input-container').children('.inline-input-accept').bind('click', function() { serverSettingSubmitForm(name, setting, id); }); $(name + '_field').closest('.inline-input-container').children('.inline-input-decline').bind('click', function() { resetForms(); $('.inline-field-placeholder').hide(); }); $(name + '_solid').hide(); } function serverSettingSubmitForm(name, setting, id) { subGroup = getSubGroupFromSetting(setting); var formData = $(name + '_field').closest("form").serialize(); $.ajax({ data: formData, cache: false, beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, success:function (data, textStatus) { $.ajax({ type:"get", url:"/servers/serverSettingsReloadSetting/" + setting + "/" + id, success:function (data2, textStatus2) { $('#' + subGroup + "_" + id + '_row').replaceWith(data2); $(".loading").hide(); }, error:function() { showMessage('fail', 'Could not refresh the table.'); } }); }, error:function() { showMessage('fail', 'Request failed for an unknown reason.'); resetForms(); $('.inline-field-placeholder').hide(); }, type:"post", url:"/servers/serverSettingsEdit/" + setting + "/" + id + "/" + 1 }); $(name + '_field').unbind("keyup"); $(name + '_form').unbind("focusout"); return false; } function updateOrgCreateImageField(string) { string = escape(string); $.ajax({ url:'/img/orgs/' + string + '.png', type:'HEAD', error: function(){ $('#logoDiv').html('No image uploaded for this identifier'); }, success: function(){ $('#logoDiv').html(''); } }); } function generateOrgUUID() { $.ajax({ url:'/admin/organisations/generateuuid.json', success: function( data ){ $('#OrganisationUuid').val(data.uuid); } }); } function sharingGroupIndexMembersCollapse(id) { $('#' + id + '_down').show(); $('#' + id + '_up').hide(); } function sharingGroupIndexMembersExpand(id) { $('#' + id + '_down').hide(); $('#' + id + '_up').show(); } function popoverStartup() { $('[data-toggle="popover"]').popover({ animation: true, html: true, }).click(function(e) { $(e.target).popover('show'); $('[data-toggle="popover"]').not(e.target).popover('hide'); }); $(document).click(function (e) { if (!$('[data-toggle="popover"]').is(e.target)) { $('[data-toggle="popover"]').popover('hide'); } }); } function changeFreetextImportFrom() { $('#changeTo').find('option').remove(); options[$('#changeFrom').val()].forEach(function(element) { $('#changeTo').append(''); }); } function changeFreetextImportCommentExecute() { $('.freetextCommentField').val($('#changeComments').val()); } function changeFreetextImportExecute() { var from = $('#changeFrom').val(); var to = $('#changeTo').val(); $('.typeToggle').each(function() { if ($( this ).val() == from) { if (selectContainsOption("#" + $(this).attr('id'), to)) $( this ).val(to); } }); } function selectContainsOption(selectid, value) { var exists = false; $(selectid + ' option').each(function(){ if (this.value == value) { exists = true; return false; } }); return exists; } function exportChoiceSelect(e) { if ($(e.target).is("input")) { return false; } var url = $(e.target).parent().data("export-url"); var elementId = $(e.target).parent().data("export-key"); var checkbox = $(e.target).parent().data("export-checkbox"); if (checkbox == 1) { if ($('#' + elementId + '_toggle').prop('checked')) { url = $('#' + elementId + '_set').html(); } } document.location.href = url; } function importChoiceSelect(url, elementId, ajax) { if (ajax == 'false') { document.location.href = url; } else { simplePopup(url); } } function freetextImportResultsSubmit(id, count) { var attributeArray = []; var temp; for (i = 0; i < count; i++) { if ($('#Attribute' + i + 'Save').val() == 1) { temp = { value:$('#Attribute' + i + 'Value').val(), category:$('#Attribute' + i + 'Category').val(), type:$('#Attribute' + i + 'Type').val(), to_ids:$('#Attribute' + i + 'To_ids')[0].checked, comment:$('#Attribute' + i + 'Comment').val(), distribution:$('#Attribute' + i + 'Distribution').val(), sharing_group_id:$('#Attribute' + i + 'SharingGroupId').val(), data:$('#Attribute' + i + 'Data').val(), data_is_handled:$('#Attribute' + i + 'DataIsHandled').val(), tags:$('#Attribute' + i + 'Tags').val() } attributeArray[attributeArray.length] = temp; } }; $("#AttributeJsonObject").val(JSON.stringify(attributeArray)); var formData = $(".mainForm").serialize(); $.ajax({ type: "post", cache: false, url: "/events/saveFreeText/" + id, data: formData, beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, success:function (data, textStatus) { window.location = '/events/view/' + id; }, complete:function() { $(".loading").hide(); }, }); } function objectTemplateViewContent(context, id) { var url = "/objectTemplateElements/viewElements/" + id + "/" + context; $.ajax({ url: url, type:'GET', beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, error: function(){ $('#ajaxContent').html('An error has occured, please reload the page.'); }, success: function(response){ $('#ajaxContent').html(response); }, complete: function() { $(".loading").hide(); }, }); } function organisationViewContent(context, id) { organisationViewButtonHighlight(context); var action = "/organisations/landingpage/"; if (context == 'members') { action = "/admin/users/index/searchorg:"; } if (context == 'events') { action = "/events/index/searchorg:"; } $.ajax({ url: action + id, type:'GET', beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, error: function(){ $('#ajaxContent').html('An error has occured, please reload the page.'); }, success: function(response){ $('#ajaxContent').html(response); }, complete: function() { $(".loading").hide(); }, }); } function organisationViewButtonHighlight(context) { $(".orgViewButtonActive").hide(); $(".orgViewButton").show(); $("#button_" + context).hide(); $("#button_" + context + "_active").show(); } function simpleTabPage(page) { $(".tabMenuSides").removeClass("tabMenuActive"); $("#page" + page + "_tab").addClass("tabMenuActive"); $(".tabContent").hide(); $("#page" + page + "_content").show(); if (page == lastPage) simpleTabPageLast(); } function simpleTabPageLast() { var summaryorgs = summaryextendorgs = remotesummaryorgs = remotesummaryextendorgs = summaryservers = ""; var orgcounter = extendcounter = remoteorgcounter = remoteextendcounter = servercounter = 0; var sgname = "[Sharing group name not set!]"; if ($('#SharingGroupName').val()) sgname = $('#SharingGroupName').val(); var sgreleasability = "[Sharing group releasability not set!]"; if ($('#SharingGroupReleasability').val()) sgreleasability = $('#SharingGroupReleasability').val(); $('#summarytitle').text(sgname); $('#summaryreleasable').text(sgreleasability); organisations.forEach(function(organisation){ if (organisation.type == 'local') { if (orgcounter > 0) summaryorgs += ", "; summaryorgs += organisation.name; if (organisation.extend == true) { if (extendcounter > 0) summaryextendorgs += ", " summaryextendorgs += organisation.name; extendcounter++; } orgcounter++; } else { if (remoteorgcounter > 0) remotesummaryorgs += ", "; remotesummaryorgs += organisation.name; if (organisation.extend == true) { if (remoteextendcounter > 0) remotesummaryextendorgs += ", " remotesummaryextendorgs += organisation.name; remoteextendcounter++; } remoteorgcounter++; } }); if (orgcounter == 0) $('#localText').hide(); if (remoteorgcounter == 0) $('#externalText').hide(); if (extendcounter == 0) summaryextendorgs = "nobody"; if (remoteextendcounter == 0) remotesummaryextendorgs = "nobody"; servers.forEach(function(server){ if (servercounter > 0) summaryservers += ", "; if (server.id != 0) { summaryservers += server.name; if (extendcounter == 0) summaryextendorgs = "none"; servercounter++; } if (server.id == 0 && server.all_orgs == true) summaryorgs = "all organisations on this instance"; }); if ($('#SharingGroupRoaming').is(":checked")) { summaryservers = "any interconnected instances linked by an eligible organisation."; } else { if (servercounter == 0) { summaryservers = "data marked with this sharing group will not be pushed."; } } $('#summarylocal').text(summaryorgs); $('#summarylocalextend').text(summaryextendorgs); $('#summaryexternal').text(remotesummaryorgs); $('#summaryexternalextend').text(remotesummaryextendorgs); $('#summaryservers').text(summaryservers); } function sharingGroupPopulateOrganisations() { $('input[id=SharingGroupOrganisations]').val(JSON.stringify(organisations)); $('.orgRow').remove(); var id = 0; var html = ''; organisations.forEach(function(org) { html = ''; html += '' + org.type + ' '; html += '' + $('
    ').text(org.name).html() + ' '; html += '' + org.uuid + ' '; html += ''; if (org.removable == 1) { html += ''; html += ' '; $('#organisations_table tr:last').after(html); id++; }); } function sharingGroupPopulateServers() { $('input[id=SharingGroupServers]').val(JSON.stringify(servers)); $('.serverRow').remove(); var id = 0; var html = ''; servers.forEach(function(server) { html = ''; html += '' + server.name + ' '; html += '' + server.url + ' '; html += ''; html += ''; html += ' '; $('#servers_table tr:last').after(html); id++; }); } function sharingGroupExtendOrg(id) { organisations[id].extend = $('#orgExtend' + id).is(":checked"); } function sharingGroupServerAddOrgs(id) { servers[id].all_orgs = $('#serverAddOrgs' + id).is(":checked"); } function sharingGroupPopulateUsers() { $('input[id=SharingGroupServers]').val(JSON.stringify(organisations)); } function sharingGroupAdd(context, type) { if (context == 'organisation') { var jsonids = JSON.stringify(orgids); url = '/organisations/fetchOrgsForSG/' + jsonids + '/' + type } else if (context == 'server') { var jsonids = JSON.stringify(serverids); url = '/servers/fetchServersForSG/' + jsonids } $("#gray_out").fadeIn(); simplePopup(url); } function sharingGroupRemoveOrganisation(id) { organisations.splice(id, 1); orgids.splice(id, 1); sharingGroupPopulateOrganisations(); } function sharingGroupRemoveServer(id) { servers.splice(id, 1); serverids.splice(id, 1); sharingGroupPopulateServers(); } function submitPicklistValues(context, local) { if (context == 'org') { var localType = 'local'; if (local == 0) localType = 'remote'; $("#rightValues option").each(function() { if (orgids.indexOf($(this).val()) == -1) { organisations.push({ id: $(this).val(), type: localType, name: $(this).text(), extend: false, uuid: '', removable: 1 }); } orgids.push($(this).val()); sharingGroupPopulateOrganisations(); }); } else if (context == 'server') { $("#rightValues option").each(function() { if (serverids.indexOf($(this).val()) == -1) { servers.push({ id: $(this).val(), name: $(this).text(), url: $(this).attr("data-url"), all_orgs: false, removable: 1 }); } serverids.push($(this).val()); sharingGroupPopulateServers(); }); } $("#gray_out").fadeOut(); $("#popover_form").fadeOut(); } function cancelPicklistValues() { $("#popover_form").fadeOut(); $("#gray_out").fadeOut(); } function sgSubmitForm(action) { var ajax = { 'organisations': organisations, 'servers': servers, 'sharingGroup': { 'name': $('#SharingGroupName').val(), 'releasability': $('#SharingGroupReleasability').val(), 'description': $('#SharingGroupDescription').val(), 'active': $('#SharingGroupActive').is(":checked"), 'roaming': $('#SharingGroupRoaming').is(":checked"), } }; $('#SharingGroupJson').val(JSON.stringify(ajax)); var formName = "#SharingGroup" + action + "Form"; $(formName).submit(); } function serverSubmitForm(action) { var ajax = {}; switch ($('#ServerOrganisationType').val()) { case '0': ajax = { 'id': $('#ServerLocal').val() }; break; case '1': ajax = { 'id': $('#ServerExternal').val() }; break; case '2': ajax = { 'name': $('#ServerExternalName').val(), 'uuid': $('#ServerExternalUuid').val() }; break; } $('#ServerJson').val(JSON.stringify(ajax)); var formName = "#Server" + action + "Form"; $(formName).submit(); } function serverOrgTypeChange() { $(".hiddenField").hide(); switch ($('#ServerOrganisationType').val()) { case '0': $("#ServerLocalContainer").show(); break; case '1': $("#ServerExternalContainer").show(); break; case '2': $("#ServerExternalUuidContainer").show(); $("#ServerExternalNameContainer").show(); break; } } function sharingGroupPopulateFromJson() { var jsonparsed = JSON.parse($('#SharingGroupJson').val()); organisations = jsonparsed.organisations; servers = jsonparsed.servers; if (jsonparsed.sharingGroup.active == 1) { $("#SharingGroupActive").prop("checked", true); } if (jsonparsed.sharingGroup.roaming == 1) { $("#SharingGroupRoaming").prop("checked", true); $('#serverList').show(); } $('#SharingGroupName').attr('value', jsonparsed.sharingGroup.name); $('#SharingGroupReleasability').attr('value', jsonparsed.sharingGroup.releasability); $('#SharingGroupDescription').text(jsonparsed.sharingGroup.description); } function testConnection(id) { $.ajax({ url: '/servers/testConnection/' + id, type:'GET', beforeSend: function (XMLHttpRequest) { $("#connection_test_" + id).html('Running test...'); }, error: function(){ $("#connection_test_" + id).html('Internal error.'); }, success: function(response){ var result = response; switch (result.status) { case 1: status_message = "OK"; compatibility = "Compatible"; compatibility_colour = "green"; colours = {'local': 'class="green"', 'remote': 'class="green"', 'status': 'class="green"'}; issue_colour = "red"; if (result.mismatch == "hotfix") issue_colour = "orange"; if (result.newer == "local") { colours.remote = 'class="' + issue_colour + '"'; if (result.mismatch == "minor") { compatibility = "Pull only"; compatibility_colour = "orange"; } else if (result.mismatch == "major") { compatibility = "Incompatible"; compatibility_colour = "red"; } } else if (result.newer == "remote") { colours.local = 'class="' + issue_colour + '"'; if (result.mismatch != "hotfix") { compatibility = "Incompatible"; compatibility_colour = "red"; } } if (result.mismatch != false) { if (result.newer == "remote") status_message = "Local instance outdated, update!"; else status_message = "Remote outdated, notify admin!" colours.status = 'class="' + issue_colour + '"'; } if (result.post != false) { var post_colour = "red"; if (result.post == 1) { post_colour = "green"; post_result = "Received sent package"; } else if (result.post == 8) { post_result = "Could not POST message"; } else if (result.post == 9) { post_result = "Invalid body"; } else if (result.post == 10) { post_result = "Invalid headers"; } else { post_colour = "orange"; post_result = "Remote too old for this test"; } } resultDiv = '
    Local version: ' + result.local_version + '
    '; resultDiv += '
    Remote version: ' + result.version + '
    '; resultDiv += '
    Status: ' + status_message + '
    '; resultDiv += '
    Compatiblity: ' + compatibility + '
    '; resultDiv += '
    POST test: ' + post_result + '
    '; $("#connection_test_" + id).html(resultDiv); //$("#connection_test_" + id).html('OK'); break; case 2: $("#connection_test_" + id).html('Server unreachable'); break; case 3: $("#connection_test_" + id).html('Unexpected error'); break; case 4: $("#connection_test_" + id).html('Authentication failed'); break; case 5: $("#connection_test_" + id).html('Password change required'); break; case 6: $("#connection_test_" + id).html('Terms not accepted'); break; case 7: $("#connection_test_" + id).html('Remote user not a sync user'); break; } } }) } function pgpChoiceSelect(uri) { $("#popover_form").fadeOut(); $("#gray_out").fadeOut(); $.ajax({ type: "get", url: "https://pgp.circl.lu" + uri, success: function (data) { var result = data.split("
    ")[1].split("
    ")[0]; $("#UserGpgkey").val(result); showMessage('success', "Key found!"); }, error: function (data, textStatus, errorThrown) { showMessage('fail', textStatus + ": " + errorThrown); } }); } function lookupPGPKey(emailFieldName) { simplePopup("/users/fetchPGPKey/" + $('#' + emailFieldName).val()); } function zeroMQServerAction(action) { $.ajax({ type: "get", url: "/servers/" + action + "ZeroMQServer/", beforeSend: function (XMLHttpRequest) { $(".loading").show(); }, success: function (data) { $(".loading").hide(); if (action !== 'status') { window.location.reload(); } else { $("#confirmation_box").html(data); openPopup("#confirmation_box"); } }, error: function (data, textStatus, errorThrown) { showMessage('fail', textStatus + ": " + errorThrown); } }); } function convertServerFilterRules(rules) { validOptions.forEach(function (type) { container = "#"+ modelContext + type.ucfirst() + "Rules"; if($(container).val() != '') rules[type] = JSON.parse($(container).val()); }); serverRuleUpdate(); return rules; } function serverRuleUpdate() { var statusOptions = ["OR", "NOT"]; validOptions.forEach(function(type) { validFields.forEach(function(field) { if (type === 'push') { var indexedList = {}; window[field].forEach(function(item) { indexedList[item.id] = item.name; }); } statusOptions.forEach(function(status) { if (rules[type][field][status].length > 0) { $('#' + type + '_' + field + '_' + status).show(); var t = ''; rules[type][field][status].forEach(function(item) { if (t.length > 0) t += ', '; if (type === 'pull') t += item; else t += indexedList[item]; }); $('#' + type + '_' + field + '_' + status + '_text').text(t); } else { $('#' + type + '_' + field + '_' + status).hide(); } }); }); }); serverRuleGenerateJSON(); } function serverRuleFormActivate(type) { if (type != 'pull' && type != 'push') return false; $('.server_rule_popover').hide(); $('#gray_out').fadeIn(); $('#server_' + type + '_rule_popover').show(); } function serverRuleCancel() { $("#gray_out").fadeOut(); $(".server_rule_popover").fadeOut(); } function serverRuleGenerateJSON() { validOptions.forEach(function(type) { if ($('#Server' + type.ucfirst() + "Rules").length) { $('#Server' + type.ucfirst() + "Rules").val(JSON.stringify(rules[type])); } else { $('#Feed' + type.ucfirst() + "Rules").val(JSON.stringify(rules[type])); } }); } function serverRulePopulateTagPicklist() { var fields = ["tags", "orgs"]; var target = ""; fields.forEach(function(field) { target = ""; window[field].forEach(function(element) { if ($.inArray(element.id, rules["push"][field]["OR"]) != -1) target = "#" + field + "pushLeftValues"; else if ($.inArray(element.id, rules["push"][field]["NOT"]) != -1) target = "#" + field + "pushRightValues"; else target = "#" + field + "pushMiddleValues"; $(target).append($('