File size: 7,492 Bytes
27867f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
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(\'<link rel="stylesheet"',
            ' href="/modules/themes/',
            self.config['display']['theme'],
            '/theme.css"',
            ' type="text/css" />',
            '<script type="text/javascript"',
            ' src="/modules/themes/',
            self.config['display']['theme'],
            '/theme.js"></script>',
            '\');',

            'if ( !window.location.search ) {',
            '$(\'#content\').html(\'<!DOCTYPE html><html><head>'
            '<title>Dashboard</title>',
            '<meta name="viewport" content="width=device-width, ',
            'minimum-scale=1.0, maximum-scale=1.0"/>',
            '<link rel=\"stylesheet\" type="text/css" href=\"/modules/dashboard/dashboard.css\">',
            '<link rel=\"stylesheet\" type=\"text/css\" href=\"/modules/table/table.css\">',
            '<script src=\"/modules/dashboard/dashboard.js\"></script>',
            '</head>\');',

            '$(\'#content\').append(\'<div id=\"logo\"></div>',
            self.get_version_div(),
            '<div id=\"dashboard\"></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(\'<img class=\"splash\" src=\"\'+logo+\'\">',
            '\');',
            '}, 2000);'
            '} else {'
            '$(\'#logo\').html(\'<img class=\"splash\" src=\"\'+logo+\'\">',
            '\');',
            '}',
            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!<br>',
                    'Latest Version ', latest_version, ' &nbsp; ',
                    '<a style=\"border: 1px; border-radius: 0.5em;',
                    ' padding-bottom: 5px; padding-top: 5px; text-decoration: none;',
                    ' color: inherit; background: var(--theme-button-hover-color);\" ',
                    ' href=\"#\" onClick=\"load_status_url(\\\'/api/upgrade?id=cabernet\\\'',
                    ');\" title=\"Upgrade Cabernet\">',
                    '<i style=\"font-size: 150%;\" class=\"md-icon\">upgrade</i>',
                    '<b>Upgrade to ', next_version, '</b> &nbsp; </a> '
                ])

        version_js = ''.join([
            '<div style=\"padding: 1em; background: var(--docked-drawer-background);',
            ' margin-left: auto;margin-right: auto;width: max-content;\">',
            'Version: ', current_version, '<br>',
            upgrade_js,
            '</div>'
        ])
        return version_js