%namespace name="lib" file="//lib.html" import="*"/> <%! from splunk.entity import buildEndpoint from splunk import rest from splunk import getDefault, auth from mako import exceptions import splunk import os import logging import urllib from splunk import util def convert_spaces_to_underscores(s): if s: new_id = '' #remove . : ( ) chars as well for e in s: if e.isalpha() or e.isdigit() or s.isspace(): new_id += e #ensure it starts with a char if not new_id[0].isalpha(): new_id = 'a'+new_id return '_'.join(new_id.split(' ')).strip() logger = logging.getLogger('splunk.apps.launcher') %> <% ## will build a uri like https://localhost:9089/servicesNS/owner/search/apps/local uri = buildEndpoint('apps/', entityName='local', namespace=getDefault('namespace'), owner=auth.getCurrentUser()['name']) try: serverResponse, serverContent = rest.simpleRequest(uri, sessionKey=splunk.getSessionKey() , getargs={'count':'-1'}) except Exception, e: raise e atomFeed = rest.format.parseFeedDocument(serverContent) #list of atom entries atom = [] show_remote = True #pop of all apps we are not interested in for entry in atomFeed: parsedRawContents = rest.format.nodeToPrimitive(entry.rawcontents) try: if entry.title == 'launcher': show_remote = util.normalizeBoolean(parsedRawContents['remote_tab']) continue except: continue if (parsedRawContents['visible'] and not util.normalizeBoolean(parsedRawContents['visible'])): continue else: atom.append(entry) %> <%def name="getname(entry, imgtype)"> <% icon_name = '' static_dir = os.path.join('etc', 'apps', entry.title, 'appserver', 'static') if imgtype == 'app': img_name_start = 'appIcon' elif imgtype == 'screenshot': img_name_start = 'screenshot' else: return '' if os.path.exists(os.path.join(os.environ['SPLUNK_HOME'], static_dir, '%s.png' % (img_name_start))): icon_name = '%s.png' % (img_name_start) else: if imgtype == 'app': icon_name = 'appIcon_null.png' elif imgtype == 'screenshot': icon_name = 'screenshot_null.png' return icon_name %> %def> <% appcount = len(atom) currentpage = 0 pages, rem = divmod(appcount,10) if rem: pages += 1 %> <%lib:add_script_block> function initLauncher(target){ $('.launcher-applist', target).mouseenter(function(){ showAppDetails(this); }); var showAppDetails = function(appLink) { //get context var context = $(appLink).parents('.appTab'); $('.appdetails', context).hide(); //hide all appdivs /* set up selected/on state for app li */ $('.selectedApp', context).removeClass('selectedApp'); $(appLink).addClass('selectedApp'); var toShow = $(appLink).attr('appToShow'); $(toShow, context).show(); } var showFirstApp = function(pageNum, context) { var appLink = $('.page', context).eq(nextPage).find('li').eq(0); showAppDetails(appLink); } var setPageCount = function(currPage, totalApps, totalPages, context){ if ( currPage == 0 ) { pageCount = "1 - "; if ( totalApps > 10 ) { pageCount = pageCount + "10"; } else { pageCount = pageCount + totalApps; } } else if ( currPage == (totalPages-1) ) { var startCount = (currPage * 10) + 1; pageCount = startCount.toString() + " - " + totalApps.toString(); } else { var startCount = (currPage * 10) + 1; var endCount = startCount + 9; pageCount = startCount.toString() + " - " + endCount.toString(); } $('.pageCount span', context).text(pageCount); } var turnPage = function(nextPage, context) { $('.page:visible', context).fadeOut('fast', function(){ $('.page', context).eq(nextPage).fadeIn('fast'); }); $('.on', context).removeClass('on'); $('.dot', context).eq(nextPage).addClass('on'); showFirstApp(nextPage, context); } /** * function to set up the paging within a passed pageContainer * @pageContainer (dom element) - page container to set up paging for */ var setUpPaging = function(pageContainer) { var context = pageContainer; var totalPages = $('.page', context).length; // determining total apps by counting the lis. not terribly sound. var totalApps = $('.page', context).find('li').length; // set up initial counter (1-10 of X) setPageCount(0, totalApps, totalPages, context); $('.launcher-right', context).click(function(){ currPageIndex = $('.page', context).index($('.page:visible',context)[0]); if ( currPageIndex == totalPages-1) { nextPage = 0; } else { nextPage = currPageIndex + 1; } turnPage(nextPage, context); setPageCount(nextPage, totalApps, totalPages, context); return false; }); $('.launcher-left', context).click(function(){ currPageIndex = $('.page', context).index($('.page:visible',context)[0]); if ( currPageIndex == 0) { nextPage = totalPages-1; } else { nextPage = currPageIndex - 1; } turnPage(nextPage, context); setPageCount(nextPage, totalApps, totalPages, context); return false; }); } /** * currently, this gathers up all page containers and passes them to the function that sets up the dirty paging (setUpPaging). * this will change when we ajaxify the splunkbase app fetching. */ $('.pageContainer').each(function(){ setUpPaging(this); }); } function doEnableAction(enable_uri, sessionID, ctrlAction, ctrlLink, ctrlName) { $.post( enable_uri, { splunk_session_id: sessionID, ctrl: ctrlAction, ctrl_link: ctrlLink, ctrl_name: ctrlName } ); } function onEnable ( t ) { Splunk.Messenger.System.getInstance().send('warn', 'restart_server', _('You must [[/manager/search/control|restart]] for the app to be enabled.')); $(t).parent('.disabledOptions').hide().siblings('.enabledOptions').show(); } $(document).bind("PageReady", function(){ var logger = Splunk.Logger.getLogger("splunk.launcher"); new initLauncher($('#tab_1')); tabify('appTabs'); % if show_remote: var xhr = $.ajax({ type: 'GET', url: 'sb', dataType: 'html', async: true, success: function(htmlresult, textStatus){ logger.info("successfully loaded app tab content"); $("#loadingSB").hide(); var html = $('#sb').replaceWith(htmlresult); new initLauncher($('#tab_2')); }, error: function(xhr, textStatus, errorThrown) { logger.error("Could not load app tab content"); } }); % endif logger.info("Launcher initialization complete"); }); %lib:add_script_block>
${_("Below you'll find Splunk Apps to get the most out of your Splunk experience. Launch one of the installed Apps or browse for more Apps on splunk.com. ")}