var KT = KT ? KT : {}; KT.hosts = KT.hosts || {}; KT.KT_AK_LABEL = 'kt_activation_keys'; KT.hosts.availableActivationKeys = {}; function ktLoadActivationKeys() { if(getSelectedEnvId() && getSelectedContentViewId()) { ktAkTab().show(); } else { ktAkTab().hide(); return; //no Katello-specific env selected } $("#ak-subscriptions-info").hide(); $("#ak-subscriptions-spinner").show(); // Retrieve the activation keys associated with the current // environment & content view. $.ajax({ type: 'get', url: foreman_url('/katello/api/v2/environments/' + getSelectedEnvId() + '/activation_keys'), data: {'content_view_id': getSelectedContentViewId()}, success: function(response) { KT.hosts.availableActivationKeys = {}; $.each(response['results'], function (i, key) { KT.hosts.availableActivationKeys[key.name] = []; }); ktAkUpdateSubscriptionsInfo(); }, error: ktErrorLoadingActivationKeys }); } function ktErrorLoadingActivationKeys(error) { $.jnotify("Error while loading activation keys from Katello", { type: "error", sticky: true }); ktAkUpdateSubscriptionsInfo(); } function ktFindParamContainer(name){ var ret; $("div#parameters .fields input[ type = 'text']").each(function () { var element = $(this); if(element.val() == name) { ret = element.closest('.fields'); return false; } return true; }); return ret; } function ktHideParams() { var param = ktFindParamContainer(KT.KT_AK_LABEL); if(param) { param.hide(); } } function getSelectedEnvId() { var dataId = $("#hostgroup_lifecycle_environment_id > option:selected").data("id"); if (dataId === undefined) { dataId = $("#hostgroup_lifecycle_environment_id").val(); } return dataId; } function getSelectedContentViewId() { var dataId = $("#hostgroup_content_view_id > option:selected").data("id"); if (dataId === undefined) { dataId = $("#hostgroup_content_view_id").val(); } return dataId; } function ktSetParam(name, value) { var paramContainer = ktFindParamContainer(name); if(value) { if(! paramContainer) { // we create the param for kt_activation_keys $("div#parameters a[target~='#global_parameters_table']").click(); paramContainer = $("div#parameters .fields").last(); paramContainer.find("input").val(name); } paramContainer.find("textarea").val(value); } else if(paramContainer) { // we remove the param by setting destroy to 1 paramContainer.find("input[ type = 'hidden' ]").val(1); } } function ktParamToAkInput() { var paramContainer = ktFindParamContainer(KT.KT_AK_LABEL); if(paramContainer) { $("#kt_activation_keys").val(paramContainer.find("textarea").val()); } } function ktAkInputToParam() { var ktActivationKeysValue = $("#kt_activation_keys").val().replace(/,\s*/g,",").replace(/,$/g,""); ktSetParam(KT.KT_AK_LABEL, ktActivationKeysValue); } function ktAkUpdateSubscriptionsInfo() { var subsInfo = $("ul#ak-subscriptions-info"); subsInfo.empty(); var selectedKeys = $("#kt_activation_keys").val().split(/,\s*/); $.each(selectedKeys, function(i, key) { if(KT.hosts.availableActivationKeys[key]) { // hack to make it working with deface var ul = "