From 05713f823aa307aa70d89b0829d0d3556583e7fd Mon Sep 17 00:00:00 2001 From: Egor Danchenkov Date: Sat, 1 Apr 2017 16:54:28 +0300 Subject: [PATCH] Show user that new version of the app is available --- index.html | 10 +- src/ui-controls.js | 285 +++++++++++++++++++++++++-------------------- 2 files changed, 166 insertions(+), 129 deletions(-) diff --git a/index.html b/index.html index 88c6211..c8553f6 100644 --- a/index.html +++ b/index.html @@ -45,6 +45,10 @@ .align-right { text-align: right; } + + #pref-btn > span > .update-available { + color: #ff0057; + } @@ -111,10 +115,10 @@ (function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { - (i[r].q = i[r].q || []).push(arguments) - }, i[r].l = 1 * new Date(); + (i[r].q = i[r].q || []).push(arguments) + }, i[r].l = 1 * new Date(); a = s.createElement(o), - m = s.getElementsByTagName(o)[0]; + m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) diff --git a/src/ui-controls.js b/src/ui-controls.js index 9aebbb2..0d39c73 100644 --- a/src/ui-controls.js +++ b/src/ui-controls.js @@ -1,117 +1,123 @@ -exports.init = function (wv, controls) { +var remote = require('electron').remote; +var Menu = remote.Menu; +var MenuItem = remote.MenuItem; +var BrowserWindow = remote.BrowserWindow; +var app = remote.app; +var shell = remote.shell; +var ipcRenderer = require('electron').ipcRenderer; - var remote = require('electron').remote; - var Menu = remote.Menu; - var MenuItem = remote.MenuItem; - var BrowserWindow = remote.BrowserWindow; - var app = remote.app; - var shell = remote.shell; - var ipcRenderer = require('electron').ipcRenderer; +var urlHandler = require('./urlHandler.js'); +var wv = window.wv; - var urlHandler = require('./urlHandler.js'); +var clickHandler = function (name, menu) { + switch (name) { + case ('backButton'): + if (wv.canGoBack()) { + wv.goBack(); + } + break; + case ('refreshButton'): + wv.reload(); + break; + case ('forwardButton'): + if (wv.canGoForward) { + wv.goForward(); + } + break; + case ('preferenceButton'): + menu.popup(remote.getCurrentWindow()); + break; + } +}; - var menu = new Menu(); +var dynamicLabel = 'Check for Updates'; - var hideAndPause = function () { - ipcRenderer.send('toggleWindow'); - wv.send('pause'); - }; - - var menuItems = [ - { - label : 'Open in browser', - click : function () { - hideAndPause(); - shell.openExternal(urlHandler.getCurrentURL()); - }, - role : 'help' +var defaultMenuItems = [ + { + label : 'Open in browser', + click : function () { + hideAndPause(); + shell.openExternal(urlHandler.getCurrentURL()); }, - { - type : 'separator' - }, - // { - // label : 'Video controls', - // role : 'help', - // submenu: [ - // { - // label: 'Play and Pause (>||)', - // enabled : false - // }, - // { - // label: 'Go 5 seconds forward (>>)', - // enabled : false - // }, - // { - // label: 'Go 5 seconds backward (<<)', - // enabled : false - // } - // ] - // }, - { - label : 'Video controls', - click : function () { - var win = new BrowserWindow({ - // width : 800, - // height : 600, - frame : true - }); + role : 'help' + }, + { + type : 'separator' + }, + { + label : 'Video controls', + click : function () { + var win = new BrowserWindow({ + // width : 800, + // height : 600, + frame : true + }); - hideAndPause(); + hideAndPause(); - var path = app.getAppPath(); - win.loadURL('file://' + path + '/views/preferences.html#media-keys'); - win.show() - }, - role : 'help' + var path = app.getAppPath(); + win.loadURL('file://' + path + '/views/preferences.html#media-keys'); + win.show() }, - { - label : 'Preferences', - click : function () { - var win = new BrowserWindow({ - // width : 800, - // height : 600, - frame : true - }); + role : 'help' + }, + { + label : 'Preferences', + click : function () { + var win = new BrowserWindow({ + // width : 800, + // height : 600, + frame : true + }); - hideAndPause(); - var path = app.getAppPath(); - win.loadURL('file://' + path + '/views/preferences.html'); - win.show() - }, - role : 'help' + hideAndPause(); + var path = app.getAppPath(); + win.loadURL('file://' + path + '/views/preferences.html'); + win.show() }, - { - label : 'Check for Updates', - click : function () { - hideAndPause(); - shell.openExternal('https://github.com/edanchenkov/MenuTube/releases'); - }, - role : 'help' + role : 'help' + }, + { + label : dynamicLabel, + click : function () { + hideAndPause(); + shell.openExternal('https://edanchenkov.github.io/MenuTube/'); }, - { - type : 'separator' + role : 'help' + }, + { + type : 'separator' + }, + { + label : 'Reload', + click : function () { + if (typeof window !== 'undefined' && + typeof window.location !== 'undefined' && + typeof window.location.reload == 'function') { + window.location.reload(); + } }, - { - label : 'Reload', - click : function () { - if (typeof window !== 'undefined' && - typeof window.location !== 'undefined' && - typeof window.location.reload == 'function') { - window.location.reload(); - } - }, - role : 'help', - accelerator : 'Cmd+R' + role : 'help', + accelerator : 'Cmd+R' + }, + { + label : 'Quit', + click : function () { + app.quit(); }, - { - label : 'Quit', - click : function () { - app.quit(); - }, - role : 'help', - accelerator : 'Cmd+Q' - } - ]; + role : 'help', + accelerator : 'Cmd+Q' + } +]; + +var hideAndPause = function () { + ipcRenderer.send('toggleWindow'); + wv.send('pause'); +}; + +var buildMenu = function (menu, menuItems) { + + menu.clear(); for (var i = 0; i < menuItems.length; i++) { var mi = menuItems[i]; @@ -126,33 +132,60 @@ exports.init = function (wv, controls) { }) ); } +}; - var clickHandler = function (name, e) { - switch (name) { - case ('backButton'): - if (wv.canGoBack()) { - wv.goBack(); - } - break; - case ('refreshButton'): - wv.reload(); - break; - case ('forwardButton'): - if (wv.canGoForward) { - wv.goForward(); - } - break; - case ('preferenceButton'): - menu.popup(remote.getCurrentWindow()); - break; - } - }; + +/* + * This should not be here, but lets make it simple for now + * TODO: Time outs and retries should be handled where this function is called, not within the function + * */ +var attempts = 5; +var checkForUpdate = function (menu, controls) { + if (attempts > 0) { + attempts--; + setTimeout(function () { + fetch('https://api.github.com/repos/edanchenkov/MenuTube/releases/latest').then(function (res) { + res.json().then(function (data) { + if (typeof data !== 'undefined' && data.hasOwnProperty('tag_name')) { + + + if (data.tag_name !== remote.app.getVersion()) { + var menuItems = defaultMenuItems.map(function (mi) { + /* + * Should check against something else probably, not label + * */ + if (mi.label === dynamicLabel) { + mi.label = '(!) New version is available'; + } + return mi; + }); + + var prefIcon = controls.preferenceButton.querySelector('i.fa'); + + prefIcon.classList.remove('fa-bars'); + prefIcon.classList.add('fa-exclamation-circle', 'update-available'); + + buildMenu(menu, menuItems); + } + } + }); + }, checkForUpdate.bind(this, menu)); + }, 2000); + } +}; + + +exports.init = function (wv, controls) { + var menu = new Menu(); + buildMenu(menu, defaultMenuItems); + checkForUpdate(menu, controls); for (var c in controls) { - var el = controls[c]; - if (el && typeof el.addEventListener === 'function') { - el.addEventListener('click', clickHandler.bind(el, c), false); + if (controls.hasOwnProperty(c)) { + var el = controls[c]; + if (el && typeof el.addEventListener === 'function') { + el.addEventListener('click', clickHandler.bind(el, c, menu), false); + } } } -} -; +};