From 04223cfedb0e31308da83ef817124330b0c49c75 Mon Sep 17 00:00:00 2001 From: Armand Tresova Date: Sun, 4 Feb 2018 14:19:02 -0500 Subject: [PATCH] Moved docs to root & added missing common.js --- {en-US/docs => docs}/Display.png | Bin {en-US/docs => docs}/Settings.png | Bin en-US/js/common.js | 91 ++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) rename {en-US/docs => docs}/Display.png (100%) rename {en-US/docs => docs}/Settings.png (100%) create mode 100644 en-US/js/common.js diff --git a/en-US/docs/Display.png b/docs/Display.png similarity index 100% rename from en-US/docs/Display.png rename to docs/Display.png diff --git a/en-US/docs/Settings.png b/docs/Settings.png similarity index 100% rename from en-US/docs/Settings.png rename to docs/Settings.png diff --git a/en-US/js/common.js b/en-US/js/common.js new file mode 100644 index 0000000..f47a49c --- /dev/null +++ b/en-US/js/common.js @@ -0,0 +1,91 @@ +$.support.cors = true; +var isUpToDateResults = true; + +function debug(msg) { + switch(typeof msg) { + case 'object': + for(var propName in msg) { + System.Debug.outputString(propName + ': ' + msg[propName]); + } + break; + case 'array': + for (var i = 0; i < msg.length; i++) { + System.Debug.outputString(msg[i]); + } + break; + default: + System.Debug.outputString(msg); + break; + } +} + +// compare two versions, return true if local is up to date, false otherwise +// if both versions are in the form of major[.minor][.patch] then the comparison parses and compares as such +// otherwise the versions are treated as strings and normal string compare is done +var VPAT = /^\d+(\.\d+){0,2}$/; +function isUpToDate(local, remote) { + if (!local || !remote || local.length === 0 || remote.length === 0) + return false; + if (local == remote) + return true; + if (VPAT.test(local) && VPAT.test(remote)) { + var lparts = local.split('.'); + while(lparts.length < 3) + lparts.push('0'); + var rparts = remote.split('.'); + while (rparts.length < 3) + rparts.push('0'); + for (var i = 0; i < 3; i++) { + var l = parseInt(lparts[i], 10); + var r = parseInt(rparts[i], 10); + if (l === r) + continue; + return l > r; + } + return true; + } else { + return local >= remote; + } +} + +function checkForUpdate() { + $.getJSON('https://api.github.com/repos/XjSv/Cryptocurrencies-Price-Tracker-Gadget/tags', function(data) { + var currentVersion = System.Gadget.version; + var latestVersion = data[0].name.slice(1); // Get the latest tag name and remove the 'v'. + isUpToDateResults = isUpToDate(currentVersion, latestVersion); + + if (!isUpToDateResults) { + var latestVersionDownloadLink = 'https://github.com/XjSv/Cryptocurrencies-Price-Tracker-Gadget/releases/download/v' + latestVersion + '/CryptocurrenciesPriceTracker.gadget' + $('#out-of-date').html('v' + latestVersion + ' is available!'); + } + }); +} + +checkForUpdate(); // Initially check for an update on init +setTimeout(checkForUpdate, 86400000); // Then check for updates every 24 hours. + +function saveSetting(theSettingName, aSettingValue) { + try { + System.Gadget.Settings.writeString(theSettingName, aSettingValue); + } catch (objException) { + debug('Write Setting Exception'); + debug(objException); + } +} + +function readSetting(theSettingName, defaultValueIfEmpty) { + var retVal = ''; + try { + retVal = System.Gadget.Settings.readString(theSettingName); + } catch (objException) { + debug('Read Setting Exception'); + debug(objException); + retVal = ''; + } + + if (retVal == '') { + return defaultValueIfEmpty; + } else { + return retVal; + } +} \ No newline at end of file