""" MIT License Copyright (C) 2023 ROCKY4546 https://github.com/rocky4546 This file is part of Cabernet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. """ import lib.common.utils as utils from lib.common.decorators import getrequest from lib.db.db_plugins import DBPlugins @getrequest.route('/api/index.js') def pages_index_js(_webserver): indexjs = IndexJS(_webserver.config) _webserver.do_mime_response(200, 'text/javascript', indexjs.get()) return True class IndexJS: def __init__(self, _config): self.config = _config self.plugin_db = DBPlugins(_config) def check_upgrade_status(self): plugin_defns = self.plugin_db.get_plugins( True, None, None) if plugin_defns: for plugin_defn in plugin_defns: latest_version = plugin_defn['version']['latest'] upgrade_available = '' if plugin_defn['external'] and latest_version != plugin_defn['version']['current']: return '$(\"#pluginStatus\").text("Upgrade");' return '' def get(self): js = ''.join([ 'var upgrading = "running"; ', 'var lookup_title = new Map(); ', 'lookup_title.set("/html/links.html", "Cabernet Links"); ', 'lookup_title.set("/api/configform?area=general", "Cabernet Settings:Internal"); ', 'lookup_title.set("/api/configform?area=streams", "Cabernet Settings:Streams"); ', 'lookup_title.set("/api/configform?area=epg", "Cabernet Settings:EPG"); ', 'lookup_title.set("/api/configform?area=clients", "Cabernet Settings:Clients"); ', 'lookup_title.set("/api/configform?area=logging", "Cabernet Settings:Logging"); ', 'lookup_title.set("/api/channels", "Cabernet Channel Editor"); ', 'lookup_title.set("/api/schedulehtml", "Cabernet Scheduler"); ', 'lookup_title.set("/api/datamgmt", "Cabernet Data Management"); ', 'lookup_title.set("/api/plugins", "Cabernet Plugins"); ', 'function load_url(url, title) {', '$(\"#content\").load(url);', 'document.title = title;', 'newurl = window.location.pathname+"?content="+encodeURIComponent(url);', 'window.history.pushState({}, null, newurl);', '}', 'function load_status_url(url) {', 'if ( upgrading == "running" ) { ', '$(\"#status\").load(url, function( resp, s, xhr ) {', 'if ( s == \"error\" ) {', '$(\"#status\").text( xhr.status + \" \" + xhr.statusText ); ', '} else {setTimeout(function(){', 'load_status_url(url);}, 700);', '}});} else if ( upgrading == "success" ) {$(\"#status\").append(\"Upgrade complete, reload page\");', ' upgrading = "running";', '} else {$(\"#status\").append(\"Upgrade aborted\"); upgrading = "running";}};', '$(document).ready(setTimeout(function(){', '$(\'head\').append(\'', '', '\');', 'if ( !window.location.search ) {', '$(\'#content\').html(\'' 'Dashboard', '', '', '', '', '\');', '$(\'#content\').append(\'
', self.get_version_div(), '
', '\');', '} else {', 'const urlSearchParams = new URLSearchParams(window.location.search);', 'const params = Object.fromEntries(urlSearchParams.entries());', 'if (params.content) {' 'load_url.call(this, params.content, ', 'lookup_title.get(params.content));', '}', '}', 'logo = getComputedStyle(document.documentElement)', '.getPropertyValue("--logo-url");', 'if ( logo == \"\" ) { ', 'setTimeout(function() {', 'logo = getComputedStyle(document.documentElement)', '.getPropertyValue("--logo-url");', '$(\'#logo\').html(\'', '\');', '}, 2000);' '} else {' '$(\'#logo\').html(\'', '\');', '}', self.check_upgrade_status(), '}, 1000));' ]) return js def get_version_div(self): manifest_list = self.plugin_db.get_repos(utils.CABERNET_ID) if not manifest_list: current_version = utils.VERSION upgrade_js = '' else: current_version = manifest_list[0]['version']['current'] next_version = manifest_list[0]['version'].get('next') if not next_version: current_version = 'TBD' next_version = 'TBD' latest_version = manifest_list[0]['version'].get('latest') if not latest_version: current_version = 'TBD' latest_version = 'TBD' if current_version == next_version: upgrade_js = '' else: upgrade_js = ''.join([ 'A new version of Cabernet is available!
', 'Latest Version ', latest_version, '   ', '', 'upgrade', 'Upgrade to ', next_version, '   ' ]) version_js = ''.join([ '
', 'Version: ', current_version, '
', upgrade_js, '
' ]) return version_js