luci-app-ddns: various fix 1865/head
authorAnsuel Smith <ansuelsmth@gmail.com>
Mon, 16 Jul 2018 01:30:43 +0000 (03:30 +0200)
committerAnsuel Smith <ansuelsmth@gmail.com>
Sun, 2 Sep 2018 12:04:31 +0000 (14:04 +0200)
Rework overview page, sync with new style and simplify the code by removing unnecessary check.

Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
applications/luci-app-ddns/Makefile
applications/luci-app-ddns/luasrc/controller/ddns.lua
applications/luci-app-ddns/luasrc/view/ddns/system_status.htm

index 195e08131b2471c54938ed9f00fbb60a10fa3b57..331851d263cfcb25fd66764838bdb8489528ae1d 100644 (file)
@@ -16,7 +16,7 @@ PKG_VERSION:=2.4.9
 
 # Release == build
 # increase on changes of translation files
-PKG_RELEASE:=5
+PKG_RELEASE:=6
 
 PKG_LICENSE:=Apache-2.0
 PKG_MAINTAINER:=Ansuel Smith <ansuelsmth@gmail.com>
index e152bb0d9844b24ce90db7309fed4d590793271a..24735289e03ccfd9db4746e622a7376436c56471 100755 (executable)
@@ -25,6 +25,8 @@ local app_name    = "luci-app-ddns"
 local app_title   = "Dynamic DNS"
 local app_version = "2.4.9-1"
 
+local translate = I18N.translate
+
 function index()
        local nxfs      = require "nixio.fs"            -- global definitions not available
        local sys       = require "luci.sys"            -- in function index()
@@ -59,40 +61,47 @@ end
 
 -- Application specific information functions
 function app_description()
-       return  I18N.translate("Dynamic DNS allows that your router can be reached with " ..
-                       "a fixed hostname while having a dynamically changing IP address.")
-               .. [[<br />]]
-               .. I18N.translate("OpenWrt Wiki") .. ": "
-               .. [[<a href="http://wiki.openwrt.org/doc/howto/ddns.client" target="_blank">]]
-               .. I18N.translate("DDNS Client Documentation") .. [[</a>]]
-               .. " --- "
-               .. [[<a href="http://wiki.openwrt.org/doc/uci/ddns" target="_blank">]]
-               .. I18N.translate("DDNS Client Configuration") .. [[</a>]]
+       local tmp = {}
+       tmp[#tmp+1] =   translate("Dynamic DNS allows that your router can be reached with \
+                                                               a fixed hostname while having a dynamically changing IP address.")
+       tmp[#tmp+1] =   [[<br />]]
+       tmp[#tmp+1] =   translate("OpenWrt Wiki") .. ": "
+       tmp[#tmp+1] =   [[<a href="https://openwrt.org/docs/guide-user/services/ddns/client" target="_blank">]]
+       tmp[#tmp+1] =   translate("DDNS Client Documentation")
+       tmp[#tmp+1] =   [[</a>]]
+       tmp[#tmp+1] =   " --- "
+       tmp[#tmp+1] =   [[<a href="https://openwrt.org/docs/guide-user/base-system/ddns" target="_blank">]]
+       tmp[#tmp+1] =   translate("DDNS Client Configuration")
+       tmp[#tmp+1] =   [[</a>]]
+       
+       return table.concat(tmp)
 end
 function app_title_back()
-       return  [[<a href="]]
-               .. DISP.build_url("admin", "services", "ddns")
-               .. [[">]]
-               .. I18N.translate(app_title)
-               .. [[</a>]]
+       local tmp = {}
+       tmp[#tmp+1] =   [[<a href="]]
+       tmp[#tmp+1] =   DISP.build_url("admin", "services", "ddns")
+       tmp[#tmp+1] =   [[">]]
+       tmp[#tmp+1] =     translate(app_title)
+       tmp[#tmp+1] =   [[</a>]]
+       return table.concat(tmp)
 end
 
 -- Standardized application/service functions
 function app_title_main()
-       tmp = {}
+       local tmp = {}
        tmp[#tmp+1] =   [[<a href="javascript:alert(']]
-       tmp[#tmp+1] =            I18N.translate("Version Information")
+       tmp[#tmp+1] =            translate("Version Information")
        tmp[#tmp+1] =            [[\n\n]] .. app_name
-       tmp[#tmp+1] =            [[\n]] .. I18N.translate("Version") .. [[: ]] .. app_version
-       tmp[#tmp+1] =            [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("required") .. [[:]]
-       tmp[#tmp+1] =            [[\n]] .. I18N.translate("Version") .. [[: ]]
-       tmp[#tmp+1] =                    srv_ver_min .. [[ ]] .. I18N.translate("or higher")
-       tmp[#tmp+1] =            [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("installed") .. [[:]]
-       tmp[#tmp+1] =            [[\n]] .. I18N.translate("Version") .. [[: ]]
-       tmp[#tmp+1] =                    (service_version() or I18N.translate("NOT installed"))
+       tmp[#tmp+1] =            [[\n]] .. translate("Version") .. [[: ]] .. app_version
+       tmp[#tmp+1] =            [[\n\n]] .. srv_name .. [[ ]] .. translate("required") .. [[:]]
+       tmp[#tmp+1] =            [[\n]] .. translate("Version") .. [[: ]]
+       tmp[#tmp+1] =                    srv_ver_min .. [[ ]] .. translate("or higher")
+       tmp[#tmp+1] =            [[\n\n]] .. srv_name .. [[ ]] .. translate("installed") .. [[:]]
+       tmp[#tmp+1] =            [[\n]] .. translate("Version") .. [[: ]]
+       tmp[#tmp+1] =                    (service_version() or translate("NOT installed"))
        tmp[#tmp+1] =            [[\n\n]]
        tmp[#tmp+1] =    [[')">]]
-       tmp[#tmp+1] =    I18N.translate(app_title)
+       tmp[#tmp+1] =    translate(app_title)
        tmp[#tmp+1] =    [[</a>]]
                
        return table.concat(tmp)
@@ -102,7 +111,7 @@ function service_version()
        
        local srv_ver_cmd = luci_helper .. " -V | awk {'print $2'} "
        local ver
-       
+
        if IPKG then
                ver = IPKG.info(srv_name)[srv_name].Version
        else
@@ -137,6 +146,7 @@ local function _get_status()
                local enabled   = tonumber(s["enabled"]) or 0
                local datelast  = "_empty_"     -- formatted date of last update
                local datenext  = "_empty_"     -- formatted date of next update
+               local datenextstat = nil
 
                -- get force seconds
                local force_seconds = DDNS.calc_seconds(
@@ -170,18 +180,22 @@ local function _get_status()
                force_seconds = (force_seconds > uptime) and uptime or force_seconds
                if pid > 0 and ( lasttime + force_seconds - uptime ) <= 0 then
                        datenext = "_verify_"
+                       datenextstat = translate("Verify")
 
                -- run once
                elseif force_seconds == 0 then
                        datenext = "_runonce_"
+                       datenextstat = translate("Run once")
 
                -- no process running and NOT enabled
                elseif pid == 0 and enabled == 0 then
                        datenext  = "_disabled_"
+                       datenextstat = translate("Disabled")
 
                -- no process running and enabled
                elseif pid == 0 and enabled ~= 0 then
                        datenext = "_stopped_"
+                       datenextstat = translate("Stopped")
                end
 
                -- get/set monitored interface and IP version
@@ -192,10 +206,12 @@ local function _get_status()
 
                -- try to get registered IP
                local lookup_host = s["lookup_host"] or "_nolookup_"
+
                local chk_sec  = DDNS.calc_seconds(
                                        tonumber(s["check_interval"]) or 10,
                                        s["check_unit"] or "minutes" )
                local reg_ip = DDNS.get_regip(section, chk_sec)
+               
                if reg_ip == "NOFILE" then
                        local dnsserver = s["dns_server"] or ""
                        local force_ipversion = tonumber(s["force_ipversion"] or 0)
@@ -212,9 +228,6 @@ local function _get_status()
                        command = command .. [[ -- get_registered_ip]]
                        reg_ip = SYS.exec(command)
                end
-               if reg_ip == "" then
-                       reg_ip = "_nodata_"
-               end
 
                -- fill transfer array
                data[#data+1]   = {
@@ -225,7 +238,8 @@ local function _get_status()
                        reg_ip   = reg_ip,
                        pid      = pid,
                        datelast = datelast,
-                       datenext = datenext
+                       datenext = datenext,
+                       datenextstat = datenextstat
                }
        end)
 
index 7fcb882a15dfb95b4b219ce81af2f59b2b31409d..31ed42b0ac5ff5fae6ba45e1fe4a5fd4416a447f 100644 (file)
 <!-- ++ BEGIN ++ Dynamic DNS ++ system_status.htm ++ -->
 <script type="text/javascript">//<![CDATA[
-       // helper to move status data to the relevant
-       // screen objects
-       // called by XHR.poll and XHR.get
-       function _data2elements(x, data) {
-               var tbl = document.getElementById('ddns_status_table');
-               // security check
-               if ( !(tbl) ) { return; }
 
-               // clear all rows
-               while (tbl.firstElementChild !== tbl.lastElementChild)
-                       tbl.removeChild(tbl.lastElementChild);
-
-               // variable for Modulo-Division use to set cbi-rowstyle-? (0 or 1)
-               var i = -1;
-               var j = 1;
-
-               // DDNS Service disabled
-               if (data[0].enabled == 0) {
-                       var txt = '<strong><font color="red"><%:DDNS Autostart disabled%></font>' ;
-                       var url = '<a href="' + data[0].url_up + '"><%:enable here%></a></strong>' ;
-                       tbl.appendChild(E('<div class="tr cbi-section-table-row cbi-rowstyle-%d">'.format(((j + i) % 2) + 1), [
-                                       E('<div class="td">', [ txt," - ", url ])
-                                       ]));
-                       i++ ;
-               }
-
-               var configuration;
-               var next_update;
-               var lookup;
-               var registered_ip;
-               var network;
-
-               for( j = 1; j < data.length; j++ )
-               {
-                       // configuration
-                       configuration = data[j].section;
-
-                       // pid
-                       // data[j].pid ignored here
-
-                       // last update
-                       // data[j].datelast ignored here
-
-                       // next update
-                       switch (data[j].datenext) {
-                               case "_empty_":
-                                       next_update = '<em><%:Unknown error%></em>';
-                                       break;
-                               case "_stopped_":
-                                       next_update = '<em><%:Stopped%></em>';
-                                       break;
-                               case "_disabled_":
-                                       next_update = '<em><%:Disabled%></em>';
-                                       break;
-                               case "_noupdate_":
-                                       next_update = '<em><%:Update error%></em>';
-                                       break;
-                               case "_runonce_":
-                                       next_update = '<em><%:Run once%></em>';
-                                       break;
-                               case "_verify_":
-                                       next_update = '<em><%:Verify%></em>';
-                                       break;
-                               default:
-                                       next_update = data[j].datenext;
-                                       break;
-                       }
-
-                       // lookup
-                       if (data[j].lookup == "_nolookup_")
-                               lookup = '<em><%:config error%></em>';
-                       else
-                               lookup = data[j].lookup;
-
-                       // registered IP
-                       switch (data[j].reg_ip) {
-                               case "_nolookup_":
-                                       registered_ip = '<em><%:config error%></em>';
-                                       break;
-                               case "_nodata_":
-                                       registered_ip = '<em><%:No data%></em>';
-                                       break;
-                               case "_noipv6_":
-                                       registered_ip = '<em><%:IPv6 not supported%></em>';
-                                       break;
-                               default:
-                                       registered_ip = data[j].reg_ip;
-                                       break;
-                       }
-
-                       // monitored interface
-                       if (data[j].iface == "_nonet_")
-                               network = '<em><%:Config error%></em>';
-                       else
-                               network = data[j].iface;
-
-                       tbl.appendChild(E('<div class="tr cbi-section-table-row cbi-rowstyle-%d">'.format(((j + i) % 2) + 1), [
-                                       E('<div class="td">', [ E('<strong>', configuration) ]),
-                                       E('<div class="td">', next_update),
-                                       E('<div class="td">', lookup),
-                                       E('<div class="td">', registered_ip),
-                                       E('<div class="td">', network)
-                                       ]));
-               }
-
-               if (tbl.firstElementChild === tbl.lastElementChild || (data[0].enabled == 0 && tbl.childNodes.length == 2) ) {
-                       var br = '<br />';
-                       var msg = '<%:There is no service configured.%>';
-                       if (tbl.firstElementChild !== tbl.lastElementChild)
-                               br = '';
-                       msg = br + msg;
-                       tbl.appendChild(E('<div class="tr cbi-section-table-row">', [
-                                       E('<div class="td">', [ E('<em>', msg) ])
-                                       ]));
-               }
-       }
-
-       // force to immediate show status (not waiting for XHR.poll)
-       XHR.get('<%=url([[admin]], [[services]], [[ddns]], [[status]])%>', null,
+       //start polling data every 30 second, this doesn't change so much
+       XHR.poll(30, '<%=url([[admin]], [[services]], [[ddns]], [[status]])%>', null,
                function(x, data) {
-                       if (data) { _data2elements(x, data); }
-               }
-       );
-
-       XHR.poll(15, '<%=url([[admin]], [[services]], [[ddns]], [[status]])%>', null,
-               function(x, data) {
-                       if (data) { _data2elements(x, data); }
+                       if (data) { 
+                               var tbl = document.getElementById('ddns_status_table');
+                               // security check
+                               if ( !(tbl) ) { return; }
+                               
+                               var rows = [];
+               
+                               // DDNS Service disabled
+                               if (data[0].enabled == 0) {
+                                       var ddns_legend = document.getElementById('ddns_status_legend');
+                                       ddns_legend.style.display='none';
+                                       rows.push([
+                                                       '<strong><font color="red"><%:DDNS Autostart disabled%></font>',
+                                                       '<a class="cbi-button cbi-button-action important" type="button" href="' + data[0].url_up + '"><%:enable here%></a></strong>'
+                                                       ]);
+                               } else {
+                                       for( j = 1; j < data.length; j++ )
+                                       {
+                                               
+                                               rows.push([
+                                                               '<strong>' + data[j].section + '</strong>', //configuration
+                                                               data[j].datenextstat ? '<em>'+data[j].datenextstat+'</em>' : '<em><%:Unknown error%></em>',
+                                                               data[j].lookup ? data[j].lookup : '<em><%:config error%></em>',
+                                                               data[j].reg_ip ? data[j].reg_ip : '<em><%:No data%></em>',
+                                                               data[j].iface        // monitored interface
+                                                               ]);
+                                       }
+                               }
+                               
+                               cbi_update_table(tbl, rows, '<%:There is no service configured.%>'); 
+                       }
                }
        );
-
+       
 //]]></script>
 
 <fieldset class="cbi-section" id="ddns_status_section">
        <legend><a href="<%=url([[admin]], [[services]], [[ddns]])%>"><%:Dynamic DNS%></a></legend>
-
-       <div class="table cbi-section-table" id="ddns_status_table">
-               <div class="tr cbi-section-table-titles">
-                       <div class="th cbi-section-table-cell"><%:Configuration%></div>
-                       <div class="th cbi-section-table-cell"><%:Next Update%></div>
-                       <div class="th cbi-section-table-cell"><%:Lookup Hostname%></div>
-                       <div class="th cbi-section-table-cell"><%:Registered IP%></div>
-                       <div class="th cbi-section-table-cell"><%:Network%></div>
-               </div>
-               <div class="tr cbi-section-table-row">
-                       <div class="td"><em><br /><%:Collecting data...%></em></div>
+       <div class="cbi-section-node">
+               <div class="table" id="ddns_status_table">
+                       <div class="tr table-titles" id="ddns_status_legend">
+                               <div class="th"><%:Configuration%></div>
+                               <div class="th"><%:Next Update%></div>
+                               <div class="th"><%:Lookup Hostname%></div>
+                               <div class="th"><%:Registered IP%></div>
+                               <div class="th"><%:Network%></div>
+                       </div>
+                       <div class="tr">
+                               <div class="td"><em><br /><%:Collecting data...%></em></div>
+                       </div>
                </div>
        </div>
 </fieldset>
-<!-- ++ END ++ Dynamic DNS ++ system_status.htm ++ -->
+<!-- ++ END ++ Dynamic DNS ++ system_status.htm ++ -->
\ No newline at end of file